#define N 10intmain(void){
intarray[N];
// Only one line !!!
qsort((void *)array, N, sizeof(int), compare);
}
Quick Sort
Compare Function
you need to #include
intcompare(constvoid *a, constvoid *b){
int A = *(int *)a;
int B = *(int *)b;
if(A < B)
return-1; //傳回 -1 代表 a < belseif(A == B)
return0; //傳回 0 代表 a = belsereturn1; //傳回 1 代表 a > b
}
Quick Sort
Compare Function
you need to #include
#define N 10intcompare(constvoid *a, constvoid *b){
int A = *(int *)a;
int B = *(int *)b;
// Oh !!!return A - B;
}
intmain(void){
intarray[N];
qsort((void *)array, N, sizeof(int), compare);
}
Quick Sort
Compare Function
you need to #include
#define N 10intcompare(constvoid *a, constvoid *b){
// Oh my Jesusreturn *(int *)a - *(int *)b;
}
intmain(void){
intarray[N];
qsort((void *)array, N, sizeof(int), compare);
}
Quick Sort
Quick Sort in String
#define N 10intcmp(constvoid *a, constvoid *b){
returnstrcmp((char *)a, (char *)b);
}
intmain(void){
char Dic[N][20];
qsort((void *)Dic, N, sizeof(Dic[0]), cmp);
}
Quick Sort
Quick Sort in Struct
#define N 10typedefstructpikachu{int Attack;
int Defense;
} Pikachu;
intcmp(constvoid *a, constvoid *b){
Pikachu A = *(Pikachu *)a;
Pikachu B = *(Pikachu *)b;
return A.attack - B.attack;
}
intmain(void){
Pikachu Pikachus[N];
qsort((void *)Pikachus, N, sizeof(Pikachus[0]), cmp);
}