快速排序

分两部分实现,函数sort()负责选定区间,函数Qsort()负责给选定区间的数值分大小

class QuickSort {
public:

    int Qsort(int* a,int low,int high){
     int key = a[low];
    while (low < high){
        while (low < high && a[high] >= key)
            --high;
        a[low] = a[high];
        while (low < high && a[low] <= key)
            ++low;
        a[high] = a[low];
    }
    a[low] = key;
    return low;
    }


    void sort(int* A,int left,int right){
        if(left < right){
            int key = Qsort(A,left,right);
            sort(A,left,key-1);
            sort(A,key+1,right);
        }        
    }

    int* quickSort(int* A, int n) {
        // write code here
        sort(A,0,n-1);       
        return A;
    }
};

你可能感兴趣的:(快速排序)