Introduction to Qsort functions
There are many kinds of sorting methods: Select Sort, bubble sort, merge sort, quick sort, etc. Look at the name. Fast sorting is a well-known sorting algorithm. Because he is fast, the system also implements this algorithm in the library, which is convenient for us to use. This is the qsort function. It is provided in the ANSI C standard, its declaration in the Stdlib.h file, is written according to the dichotomy, its time complexity is N*log (n)
features : Sorting using the quick sort Routines
header file :stdlib.h
usage : void qsort (void* base, size_t num,size_t width,int(__cdecl* Compare) (const void*,const void*));
parameters : 1 array to sort
2 number of elements to be sorted in the array
3 space Size (in bytes) for each element
4 pointer to function to determine order of ordering (requires user to customize a comparison function)
Qsort requires a comparison function that is defined by itself . The comparison function makes the qsort more versatile, with the comparison function qsort can be sorted in ascending or descending order of structures such as arrays, strings, structures, etc.
If the comparison function int cmp (const void *a, const void *b) has two elements as arguments (the format of the parameter cannot be changed) , returns an int value that is considered a > b if the return value is greater than 0,qsort, or a < b if the return value is less than 0,qsort. Qsort know the size of the element, you can put the large front to go. In simple terms, the function of the comparison function CMP is to show qsort how the size of the element is compared.
Several common comparison function CMP in Qsort
First, sort the array of int type
intnum[ -];intCmp_int (Const void* _a,Const void* _b)//parameter format fixed{ int* A = (int*) _a;//Forcing type conversions int* B = (int*) _b; return*a-*b; }qsort (num, -,sizeof(num[0]), cmp_int);
Second, sort char array (same int type)
Charword[ -];intCmp_char (Const void* _a,Const void* _b)//parameter format fixed{ Char* A = (Char*) _a;//Forcing type conversions Char* B = (Char*) _b; return*a-*C; }qsort (Word, -,sizeof(word[0]), Cmp_char);
Third, sort the array of double type
Double inch[ -];intCmp_double (Const void* _a,Const void* _b)//parameter format fixed{ Double* A = (Double*) _a;//Forcing type conversions Double* B = (Double*) _b; return*a > *b?1: -1;//Special attention}qsort (inch, -,sizeof(inch[0]), cmp_double);
Iv. Sorting strings
Charword[ -][Ten];intCmp_string (Const void* _a,Const void* _b)//parameter format fixed{ Char* A = (Char*) _a;//Forcing type conversions Char* B = (Char*) _b; returnstrcmp (A, b);} Qsort (Word, -,sizeof(word[0]), cmp_string);
Reference: Http://wenku.baidu.com/link?url=9dEkcBiIyzIhRtpzqLd0W_sq-oQA6jiJRSlsWH7P8WmAsvsZdw3rPFAxoOOaR_ Qiht6ozj9fdl0gunuyzw2wfd-eajvccbp7vmh37ht8ohk
Http://www.cnblogs.com/syxchina/archive/2010/07/29/2197382.html
Http://www.cnblogs.com/BeyondAnyTime/archive/2012/05/22/2513776.html
C Language Qsort function usage