快速排序

#include <iostream>

using namespace std;

void Print(int arr[], int n) {
    for (int i = 0; i < n; ++i) {
        cout << arr[i] << ‘ ‘;
    }
    cout << endl;
}

int Pivot(int arr[], int left, int right) {
    int key = arr[left];
    int l = left, r = right;

    while (l < r) {
        while (l < r && key <= arr[r]) {
            --r;
        }
        arr[l] = arr[r];

        while (l < r && key >= arr[l]) {
            ++l;
        }
        arr[r] = arr[l];
    }

    arr[l] = key;
    return l;
}

void QuickSort(int arr[], int left, int right) {
    if (left < right) {
        int p = Pivot(arr, left, right);
        QuickSort(arr, left, p - 1);
        QuickSort(arr, p + 1, right);
    }
}

int main() {
    int n = 10;
    int arr[] = {5,3,1,7,2,4,8,6,3,4};
    cout << "before sorted: ";
    Print(arr, n);
    cout << "after sorted: ";
    QuickSort(arr, 0, n - 1);
    Print(arr, n);

    system("pause");
    return 0;
}

快速排序

上一篇:[转载]JavaEE学习篇之——JQuery技术详解


下一篇:Android开发 build.gradle的使用记录