标签:
#include <stdlib.h> #include <stdio.h> int partition(int a[], int p, int q){ int pivot = rand() % (q-p+1) + p; int i = p; int j = q; int key = a[pivot]; while(i < j){ while(i < j && a[i] < key){ i++; } if(i < j){ a[pivot] = a[i]; a[i] = key; pivot = i; } while(i < j && a[j] >= key){ j--; } if(i < j){ a[pivot] = a[j]; a[j] = key; pivot=j; } } return pivot; } void quickSort(int a[], int p, int q){ if (p <= q){ int r = partition(a, p, q); quickSort(a, p, r-1); quickSort(a, r+1,q); } } int main(){ int a[] = {1,2,9,2,2,2,2,3,10,4,5,7,4}; quickSort(a, 0, 12); int i = 0; }
标签:
原文地址:http://www.cnblogs.com/Key-Ky/p/4335167.html