voidswap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; }
intpartition(int *array, int low, int high) { int base = array[low]; // 用子表的第一个记录做枢轴(分水岭)记录 while(low < high) { while(array[high] >= base && low < high) { high--; } swap(&array[high], &array[low]); while(array[low] <= base && low < high) { low++; } swap(&array[high], &array[low]); } return low; }
voidquick_sort(int *array, int low, int high) { if(low < high) { int part = partition(array, low, high); quick_sort(array, low, part - 1); quick_sort(array, part + 1, high); } }
voidprint(int *array, int n) { int i; for(i = 0; i < n; i++) { printf("%d\n", array[i]); } }
// 顺序查找 intseq(int *array, int low, int high, int key) {// 在指定范围内寻找和key相同的值,找到返回下标,找不到返回-1 int i; for(i = low; i < high; i++) { count++; if(array[i] == key) { return i; } }