参考原文:https://www.cnblogs.com/CCBB/archive/2010/01/15/1648827.html
http://www.cplusplus.com/reference/cstdlib/qsort/
1.函数原型:
void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*)); /*base: *Pointer to the first object of the array to be sorted, converted to a *void*. */ /*num: *Number of elements in the array pointed to by base. *size_t is an unsigned integral type. */ /*size: *Size in bytes of each element in the array. *size_t is an unsigned integral type. */ /*compar: int compar (const void* p1, const void* p2); *如果返回值<0, p1指向的元素排在p2指向的元素之前 *如果==0,两元素 *返回值>0,p1指向的元素排在p2指向的元素之前 */
2.使用例子
/* qsort example */ #include <stdio.h> /* printf */ #include <stdlib.h> /* qsort */ int values[] = { 40, 10, 100, 90, 20, 25 }; int compare (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } int main () { int n; qsort (values, 6, sizeof(int), compare); for (n=0; n<6; n++) printf ("%d ",values[n]); return 0; }
输出为 :
10 20 25 40 90 100