快速排序

分兩部分實現,函數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;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章