Use of the Qsort function-sort elements in a normal array, pointer array, two-dimensional array

Source: Internet
Author: User
Tags strcmp

In ANSI C, the prototype of the Qsort function is

#include <stdlib.h>

void Qsort (void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *));

Explanation: The Qsort function sorts an array containing nmemb elements, while the base pointer points to the first element of the array. The number of elements in this array is specified by size.

The Compar function defines the comparison of qsort, so you can customize the comparison of numbers, the comparison of strings, and even the comparison of structures, and so on. Compar's writing is shown in the following examples.

First, use the Qsort function to sort the normal array.

The code is as follows:

1#include <stdio.h>2#include <stdlib.h>3 4 intCompareConst void*ARG1,Const void*arg2);5 6 int 7MainintargcChar**argv)8 {9     inti;Ten      One     intarr[5] = { -, -,2,7, in }; A      -Qsort (arr,sizeof(arr)/sizeof(arr[0]),sizeof(int), compare); -      the      for(i =0; I <5; i++) { -printf"%d", Arr[i]); -     } -printf"\ n"); + } -  +  A intCompareConst void*ARG1,Const void*arg2) { at     intA = * (int*) arg1; -     intb = * (int*) arg2; -     if(A >b) { -         return 1; -     } -     Else if(A <b) { in         return-1; -     } to     Else { +         return 0; -     } the}

Speaking of the definition of parameter compar, because qsort requires a function pointer, so the name of this function can be set by itself, as with the names of other formal parameters, generally with CMP or compare.

The Qsort function passes our incoming ARR as a pointer to the first element of the array, so accordingly, in line 22nd of the code, ARG1 and arg2 in the formal parameter list are pointers to an element of an array .

In lines 23rd and 24th, we convert the void* to int* and assign the values after the pointer dereference to a and b for comparison.

If A>b returns 1,A<B-1, it means that we sort the elements in ascending order and sort the elements from small to large.

Second, use the Qsort function to sort the array of pointers.

The code is as follows:

1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4 5 intCompareConst void*ARG1,Const void*arg2);6 7 int 8MainintargcChar**argv)9 {Ten     inti; One      A     Char*arr[5] = {"I"," Love","C","Programming","language" }; -      -Qsort (arr,sizeof(arr)/sizeof(arr[0]),sizeof(Char*), compare); the      -      for(i =0; I <5; i++) { -printf"%s", Arr[i]); -     } +printf"\ n"); - /* GETCHAR (); */ + } A  at intCompareConst void*ARG1,Const void*arg2) { -     Char*a = * (Char**) arg1; -     Char*b = * (Char**) arg2; -     intresult =strcmp (A, b); -     if(Result >0) { -         return 1; in     } -     Else if(Result <0) { to         return-1; +     } -     Else { the         return 0; *     } $}

The array of pointers is somewhat difficult to understand compared to the usual array of arrays, in fact, the ARR array on line 12th, which is a 5 pointer, the 1th element holds the address to the constant string "I", the 2nd element holds the address to the constant string "love", and so on.

So after we passed arr to Qsort, Qsort understood arr as a pointer to the first element in the array, so arg1 and arg2 are actually pointers to " pointers to constant strings " in the 23rd row parameter table , is char**. And we need to pass the string comparison function to strcmp, which is "pointer to string", is char*, so we convert void* to char**, then dereference, get char*, give A and B. Next, use strcmp to compare A to B.

Third, use the Qsort function to sort the array of pointers.

The code is as follows:

1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4 5 intCompareConst void*ARG1,Const void*arg2);6 7 int 8MainintargcChar**argv)9 {Ten     inti; One      A     Chararr[5][ -] = {"I"," Love","C","Programming","language" }; -      -Qsort (arr,sizeof(arr)/sizeof(arr[0]),sizeof(arr[0]), compare); theprintf"%s\n", arr[0]); -      for(i =0; I <5; i++) { -printf"%s", Arr[i]); -     } +printf"\ n"); - /* GETCHAR (); */ + } A  at intCompareConst void*ARG1,Const void*arg2) { -     Char*a = (Char*) arg1; -     Char*b = (Char*) arg2; -     intresult =strcmp (A, b); -     if(Result >0) { -         return 1; in     } -     Else if(Result <0) { to         return-1; +     } -     Else { the         return 0; *     } $}

A two-dimensional array is common, but it is slightly more difficult to sort the elements in the Qsort function in order to understand them. But step by step analysis, the idea is natural.

In this case, the two-dimensional array is sorted, in fact, the second dimension of the two-dimensional array of strings stored in the order. So in the 14th row of calls to the Qsort function, the second parameter is the number of elements to be queued (5), and the third parameter is the size of the element to be queued (16).

We pass arr into the Qsort function, the Qsort function will interpret arr as a pointer to the first element of the array, and the first element of Arr is arr[0][0], so the parameters arg1 and arg2 refer to pointers to "A[i][0 ", and we know that a[ I][0] is a character, which is char, so arg1 and arg2 refer to char *. We convert void* to char*, give A and B, and call the strcmp function to compare A and B.

That's it.

Use of the Qsort function-sort elements in a normal array, pointer array, two-dimensional array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.