首页 > 编程语言 > 详细

快速排序

时间:2021-03-10 10:04:49      阅读:47      评论:0      收藏:0      [点我收藏+]

理论部分可参考:

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

 

快速排序

原文:https://www.cnblogs.com/sbj123456789/p/14509458.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!