编译环境:gcc4.4.3
#include <stdio.h> #define MAX 10 #define swap(a,b) {if(a!=b){(a)^=(b);(b)^=(a);(a)^=(b);}} //此函数用于保证x左边的数都小于它,而右边的数都大于它。 int partition(int *A, int p, int r) { int x, i, j; x = A[r]; i = p - 1; for (j = p; j < r; j++) { if (A[j] <= x) { i++; swap(A[j], A[i]); } } swap(A[i+1], A[r]); return i+1; } void quick_sort(int *A, int p, int r) { int q; if (p < r) { q = partition(A, p, r); quick_sort(A, p, q-1); quick_sort(A, q+1, r); } } void prtarry(int* sum, int n) { int i; printf("array: "); for(i = 0; i < n; i++) { printf("%2d ", sum[i]); } printf("\n"); } int main(int argc, const char *argv[]) { int sum[MAX] = {5,7,8,2,4,6,9,0,1,3}; prtarry(sum, MAX); quick_sort(sum, 0, MAX-1); prtarry(sum, MAX); return 0; }
转载于:https://www.cnblogs.com/tonykong/archive/2013/02/05/quick_sort.html