美文网首页
快速排序

快速排序

作者: GengJianQi | 来源:发表于2018-08-15 08:03 被阅读0次
    def partition(data, left, right):
        pivot = data[left]
        i = left
        j = right
    
        while i < j:
            while i < j and data[j] >= pivot:
                j -= 1
            while i < j and data[i] <= pivot:
                i += 1
            if i < j:
                data[i], data[j] = data[j], data[i]
    
        data[left] = data[i]
        data[i] = pivot
    
        return i
    
    
    def quick_sort1(data, left, right):
        if left >= right:
            return
        i = partition(data, left, right)
        quick_sort1(data, left, i-1)
        quick_sort1(data, i+1, right)
    
    
    def quick_sort2(data):
        if len(data) in (0, 1):
            return
        s = [0, len(data)-1]
        while s:
            right = s.pop()
            left = s.pop()
            if left >= right:
                continue
            i = partition(data, left, right)
            s.append(left)
            s.append(i-1)
            s.append(i+1)
            s.append(right)
    

    相关文章

      网友评论

          本文标题:快速排序

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