1. 程式人生 > >資料結構與演算法: 快速排序

資料結構與演算法: 快速排序

    void quickSort(vector<int> &arra, int L, int R) {
        int *p;
        p = partition2(arra, L, R);
        cout << p[0] << endl;
        quickSort(arra, L, p[0] - 1);
        quickSort(arra, p[1] + 1, R);
        delete p;
    }



    int *partition2(vector<int> &arr, int L, int R) {
        int less = L - 1;
        int more = R;
        while (L < more) {
            if (arr[L] < arr[R]) {
                swap(arr[++less], arr[L++]);

            }
            else if (arr[L] > arr[R]) {
                swap(arr[--more], arr[L]);
            }
            else {
                L++;
            }

        }
        swap(arr[more], arr[R]);
        int *temp = new int[2];
        temp[2] = {less+1, more};
        return temp;
    }