快排

class Solution {
public:
    /**
     * @param A an integer array
     * @return void
     */
    void sortIntegers2(vector<int>& A) {
        // Write your code here
        /*
        vector<int>temp(A.size(),0);
        mergeSort(A,temp,0,A.size()-1);
        */
        quickSort(A,0,A.size()-1);
    }
    
    
    
  void quickSort(vector<int>& A,int left,int right) {
        if(left >= right) { 
            return;
        }
        int index = partion(A,left,right);
        int temp = A[index];
        A[index] = A[left];
        A[left] = temp;
        quickSort(A,left,index-1);
        quickSort(A,index+1,right);
}
    
    int partion(vector<int>& A,int left,int right) {
        int pivot = A[left];
        int i = left;
        int j = right+1;
        while(i < j ) {
            while(A[--j]>pivot) {
                
            }
            while((i<j) && (A[++i]<pivot)) {
              
            }
            int temp = A[i];
            A[i] = A[j];
            A[j] = temp;
        };
//返回i和j都可以,注意分析left+1 = right这种情况
        return j;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容