标签:
#include <iostream> #include <algorithm> #include <cstdio> #include <stack> using namespace std; void quicksort(int* a, int from, int to); void quicksort(int* a, int length); void quicksort_nonrecursive(int* a, int from, int to); int partition(int* a, int from, int to); void quicksort(int* a, int from, int to) { if (from<to) { int pivot = partition(a, from, to); quicksort(a, from, pivot-1); quicksort(a, pivot+1, to); } } void quicksort(int* a, int length) { return quicksort(a,0,length-1); } int partition(int* a, int from, int to) { int x = a[to]; int i = from-1; for(int j = from; j<=to-1; ++j) { if (a[j]<=x) { ++i; swap(a[i],a[j]); } } swap(a[i+1],a[to]); return i+1; } int main() { int i[9] = {3, 0, 6, 6, 7, 4, 6, 9, 8}; quicksort(i,9); for(int j = 0; j<9; j++) { printf("%d\t",i[j]); } }
标签:
原文地址:http://www.cnblogs.com/roy-mustango/p/4224955.html