快排算法 ,打印快排过程:
#include <iostream> using namespace std; void PrintArr(int * arr, int length) { for (int i = 0; i < length; i++) { cout << " " << arr[i] << " "; } cout << endl; } void QuickSort(int *arr, int nfirst, int nlast) { if (nfirst > nlast) { return; } int first = nfirst; int last = nlast; int key = arr[first]; while (first < last) { while (first < last && arr[last] >= key) --last; arr[first] = arr[last]; PrintArr(arr, 16); while (first < last && arr[first] <= key) ++first; arr[last] = arr[first]; PrintArr(arr, 16); } arr[first] = key; PrintArr(arr, 16); cout <<"----------------------------------" <<endl; QuickSort(arr, nfirst, first - 1); QuickSort(arr, first + 1, nlast); } void main() { int arr[] = { 5, 6, 3, 7, 1, 2, 5, 3, 2, 7, 4, 9, 2, 6, 3, 4 }; QuickSort(arr, 0, 15); cout << endl << endl; PrintArr(arr,16); getchar(); }