美文网首页工作生活
快速排序 Hoare partition scheme

快速排序 Hoare partition scheme

作者: yingjieg | 来源:发表于2019-07-01 19:23 被阅读0次
    Hoare partition scheme
    import random
    nums = []
    for x in range(10):
        nums.append(random.randint(1, 101))
    
    print(nums)
    
    def partition(arr, low, high, pivot):
        while low <= high:
            while arr[low] < pivot:
                low += 1
    
            while arr[high] > pivot:
                high -= 1
    
            if low <= high:
                arr[low], arr[high] = arr[high], arr[low]
                low += 1
                high -= 1
    
        return low
    
    def quicksort(arr, low, high):
        if low >= high:
            return
    
        pivot = arr[low]
    
        p = partition(arr, low, high, pivot)
    
        quicksort(arr, low, p - 1)
        quicksort(arr, p, high)
    
    
    quicksort(nums, 0, len(nums) - 1)
    
    print(nums)
    

    相关文章

      网友评论

        本文标题:快速排序 Hoare partition scheme

        本文链接:https://www.haomeiwen.com/subject/fdbncctx.html