功 能: 快速排序
头文件:stdlib.h
用 法: void qsort(void *base,int nelem,int width,int (*fcmp)(const void *,const void *));
参数:
1 待排序数组首元素的地址
2 数组中待排序元素数量
3 各元素的占用空间大小
4 指向函数的指针,用于确定排序的顺序
注意:数组中可以存储数字,字符,或者结构体都行。
数字:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int compInc(const void *a, const void *b) 5 { 6 return *(int *)a - *(int *)b; 7 } 8 9 int compDec(const void *a, const void *b) 10 { 11 return *(int *)b - *(int *)a; 12 } 13 14 15 int main() 16 { 17 int a[5] = { 11,2,13,4,7 }; 18 int len = 5; 19 int i; 20 21 printf("递增排序结果:\n"); 22 qsort(a, len, sizeof(a[0]), compDec); 23 for (i = 0; i < len; i++) 24 { 25 printf("%d ", a[i]); 26 } 27 printf("\n"); 28 29 return 0; 30 }
字符串长度排序:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #define M 12 5 #define N 20 6 7 int compareInc(const void *a, const void *b) 8 { 9 return strlen((char *)a) - strlen((char*)b);//字符串长度比较 10 } 11 12 int compareDec(const void *a, const void *b) 13 { 14 return strlen((char *)b) - strlen((char*)a); 15 } 16 17 int main(void) 18 { 19 int i; 20 char s[M][N] = 21 { 22 "January", 23 "February", 24 "March", 25 "April", 26 "May", 27 "June", 28 "July", 29 "August", 30 "September", 31 "October", 32 "November", 33 "December" 34 }; 35 36 qsort(s, M, sizeof(char) * N, compareInc); 37 for (i = 0; i < M; i++) 38 printf("%s\n", s[i]); 39 40 printf("\n"); 41 42 qsort(s, M, sizeof(char) * N, compareDec); 43 for (i = 0; i < M; i++) 44 printf("%s\n", s[i]); 45 46 return 0; 47 }
字符串大小排序:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #define M 12 5 #define N 20 6 7 8 int compare1(const void *a, const void *b) 9 { 10 return *(char *)a - *(char*)b;//字符串大小比较 11 } 12 13 int compare2(const void *a, const void *b) 14 { 15 return *(char *)b - *(char*)a; 16 } 17 18 int main(void) 19 { 20 int i; 21 char s[M][N] = 22 { 23 "January", 24 "February", 25 "March", 26 "April", 27 "May", 28 "June", 29 "July", 30 "August", 31 "September", 32 "October", 33 "November", 34 "December" 35 }; 36 37 qsort(s, M, sizeof(char) * N, compare1); 38 for (i = 0; i < M; i++) 39 printf("%s\n", s[i]); 40 41 printf("\n"); 42 43 qsort(s, M, sizeof(char) * N, compare2); 44 for (i = 0; i < M; i++) 45 printf("%s\n", s[i]); 46 47 return 0; 48 }
结构体:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define N 6 4 5 typedef struct 6 { 7 char name[15]; 8 int score; 9 10 }Student; 11 12 int compare1(const void *a, const void *b) 13 { 14 return ((Student*)a)->score - ((Student*)b)->score;//以结构体的成绩排序 15 } 16 17 int compare2(const void *a, const void *b) 18 { 19 return *(((Student*)a)->name) - *(((Student*)b)->name);//以结构体的学生姓名排序 20 } 21 22 void print(Student s) 23 { 24 printf("%-15s : %d\n", s.name, s.score); 25 } 26 27 int main() 28 { 29 Student s[N] = 30 { 31 "Zhang San", 94, 32 "Li Si", 80, 33 "You", 94, 34 "I", 100, 35 "He", 72, 36 "She", 60 37 }; 38 39 int i; 40 qsort(s, N, sizeof(Student), compare1); 41 for (i = 0; i < N; i++) 42 { 43 print(s[i]); 44 } 45 46 printf("\n"); 47 48 qsort(s, N, sizeof(Student), compare2); 49 for (i = 0; i < N; i++) 50 { 51 print(s[i]); 52 } 53 54 return 0; 55 }