源代码:
#include <iostream> void QuickSort(int arr[], int start, int end); void swap(int& a, int& b); int main() { int a[] = { 7,12,5,8,4,2,10,15 }; int size = sizeof(a) / sizeof(int); QuickSort(a,0,size-1); for (int i = 0; i < size; i++) { std::cout << a[i] << ", "; } std::cout << std::endl; } void swap(int& a, int& b) { int tmp = a; a = b; b = tmp; } void QuickSort(int arr[],int start,int end) { if (start > end) return; int k = arr[start]; int i = start, j = end; while (i < j) { while (j > i && arr[j] > k) --j; swap(arr[i],arr[j]); while (i < j && arr[i] < k) ++i; swap(arr[i], arr[j]); } QuickSort(arr,start,i-1); QuickSort(arr, i + 1, end); }