xiaoboalex网友贴的代码,暂时没看,过几天再看下,当是存起来吧:
#define NULL 0 #include <iostream> using namespace std; void swap(int arr[], int i, int j) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } void move(int arr[], int s, int e, int& i, int delta) { i += delta; while (i >= s && i <= e && NULL == arr[i]) i += delta; } void quick_sort(int arr[], int s, int e) { if (s >= e) return; int key = arr[s]; int i = s, j = e; while (NULL == arr[i]) move(arr, s, e, i, 1); while (NULL == arr[j]) move(arr, s, e, j, -1); if (i >= j) return; while (i < j) { while (i < j && arr[j] >= key) move(arr, s, e, j, -1); swap(arr, i, j); while (i < j && arr[i] <= key) move(arr, s, e, i, 1); swap(arr, i, j); } quick_sort(arr, s, i-1); quick_sort(arr, i+1, e); } int main () { int arr[9] = {9, 4, NULL, 6, 2, NULL, NULL, 7, 3}; quick_sort(arr, 0, 8); for (int i=0; i<9; ++i) cout<<arr[i]<<" "; cout<<endl; system("pause"); return 0; }