C++标准快速排序库qsort进行结构体快速排序
代码如下
1 #include <stdio.h> 2 #include <stdlib.h> 3 typedef struct 4 { 5 int date; 6 int value; 7 }X_S; 8 9 /* a->z */ 10 int cmpfunA2Z(const void * a, const void * b) 11 { 12 X_S * p1 = (X_S *)a; 13 X_S * p2 = (X_S *)b; 14 15 return p1->value > p2->value; 16 } 17 18 /* z -> a */ 19 int cmpfunZ2A(const void * a, const void * b) 20 { 21 X_S * p1 = (X_S *)a; 22 X_S * p2 = (X_S *)b; 23 24 return p1->value < p2->value; 25 } 26 27 int quickSortOfCpp() 28 { 29 X_S Xlist[100]; 30 int Ilist[100]; 31 int i = 0; 32 for(i = 0; i < 100; i++) 33 { 34 Xlist[i].date = i+1; 35 double t = (double)i - 50.3; 36 Xlist[i].value = (int)(t * t + 5.6); 37 } 38 39 for(i = 0; i < 100; i++) 40 { 41 printf("num : %3d, value : %4d ", Xlist[i].date, Xlist[i].value); 42 } 43 44 qsort(Xlist, 100, sizeof(X_S), cmpfunA2Z); 45 printf("