快速排序

理论部分可参考:

https://labuladong.github.io/algo/%E7%AE%97%E6%B3%95%E6%80%9D%E7%BB%B4%E7%B3%BB%E5%88%97/%E5%BF%AB%E9%80%9F%E9%80%89%E6%8B%A9.html

 

这里给出一个简洁的Python解:

def MySort(self , arr ):
        
        def QuickSort(arr, lo, hi):
            if lo >= hi:
                return
            
            p = Partition(arr, lo, hi)
            QuickSort(arr, lo, p-1)
            QuickSort(arr, p+1, hi)

        def Partition(arr, lo ,hi):
            if lo == hi:
                return lo
            pivot = arr[lo]
            left = lo 
            right = hi
            while(left < right):
                while arr[right] >= pivot and left < right:
                    right -= 1
                arr[left] = arr[right]
                while arr[left] <= pivot and left < right:
                    left += 1
                arr[right] = arr[left]
            
            arr[left] = pivot
            return left
        
        QuickSort(arr, 0, len(arr)-1)
        
        return arr

 

posted @ 2021-03-10 09:00  sbj123456789  阅读(108)  评论(0编辑  收藏  举报