# include <stdio.h> void Quick_sort(int *, int, int); int Find_pos(int *, int, int); void Sort(int *, int, int); int main (void){ int a[5] = {2, 5, 0, 1, 4}; Sort(a, 0, 4); Quick_sort(a, 0, 4); Sort(a, 0, 4); return 0; } void Quick_sort(int * a, int low, int high){ int pos; if(low < high){ pos = Find_pos(a, low, high); Quick_sort(a, low, pos-1); Quick_sort(a, pos+1, high); } } int Find_pos(int * a, int low, int high){ int val = a[low]; while (low < high){ while (low < high && a[high] > val) --high; a[low] = a[high]; while (low < high && a[low] < val) ++low; a[high] = a[low]; } a[low] = val; // 循环结束时 low = high return high; } void Sort(int * a, int low, int high){ for (int i = low; i <= high; ++i) { printf("%d ", a[i]); } printf("\n"); }
排序——快速排序
最新推荐文章于 2024-08-16 16:00:00 发布