找了一下以前的帖子,竟没有,补一下。
/home/a/j/nomad2:cat quicksort.c #include <stdio.h> int partition(int *a, int m, int n) { int low = m; int high = n; int pivot = low; int pvalue = a[pivot]; while(low < high) { while(low < high && a[high] >= pvalue) { high--; } a[low] = a[high]; while(low < high && a[low] <= pvalue) { low++; } a[high] = a[low]; } a[low] = pvalue; return low; } void quicksort(int *a, int m, int n) { int pivot; if ( m < n ) { pivot = partition(a,m,n); quicksort(a, m, pivot-1); quicksort(a, pivot+1, n); } } int main() { int a[] = {49, 38, 65, 97, 76, 13, 27, 49}; int n = sizeof(a)/sizeof(int); int i = 0; quicksort(a, 0, n-1); for(i = 0; i < n; i++) { printf("%d ",a[i]); } printf("\n"); }
/home/a/j/nomad2:./a.out 13 27 38 49 49 65 76 97