快速排序时目前被认为是最好的一种内部排序方法。同时,快速排序被认为是,在所有同数量级(O(nlogn))的排序方法中,其平均性能最好。 其具体的C实现代码如下所示: #include <stdio.h> int Partition(int *a,int low,int high) { int basekey = a[low]; while(low < high) { while(low < high && a[high] >= basekey) { high--; } a[low] = a[high]; while(low < high && a[low] <= basekey) { low ++; } a[high] = a[low]; } a[low] = basekey; return low; } void QSort(int *a,int low ,int high) { int loc; if(low < high) { loc = Partition(a,low,high); QSort(a,low,loc-1); QSort(a,loc+1,high); } } void QuickSort(int *a,int high) { QSort(a,0,high); } void main() { int b[10] = {30,5,6,7,22,56,28,19,8,11}; QuickSort(b,9); for(int i = 0 ; i < 10 ; i++) { printf("%d ",b[i]); } }