Quick sort (Python detailed notes)

step

1. Pick an element from the sequence and call it a "reference" (sentinel) (pivot).
2. Reorder the sequence so that all elements smaller than the reference value are placed in front of the reference, and all elements larger than the reference value are placed in front of the reference. Behind the benchmark (the same number can go to either side). After this partition is over, the benchmark is in the middle of the sequence. This is called a partition operation.
3. Recursively sort the sub-sequences of elements smaller than the reference value and the sub-sequences of elements greater than the reference value.
The bottom case of recursion is that the size of the sequence is zero or one, that is, it has always been sorted. Although it has been recursive, this algorithm will always end, because in each iteration, it will put at least one element to its last position.
process

Code:

def quick_sort(alist, start, end):
    """快速排序"""
    if start >= end:  # 递归的退出条件
        return
    mid = alist[start]  # 设定起始的基准元素
    low = start  # low为序列左边在开始位置的由左向右移动的游标
    high = end  # high为序列右边末尾位置的由右向左移动的游标
    while low < high:
        # 如果low与high未重合,high(右边)指向的元素大于等于基准元素,则high向左移动
        while low < high and alist[high] >= mid:
            high -= 1
        alist[low] = alist[high]  # 走到此位置时high指向一个比基准元素小的元素,将high指向的元素放到low的位置上,此时high指向的位置空着,接下来移动low找到符合条件的元素放在此处
        # 如果low与high未重合,low指向的元素比基准元素小,则low向右移动
        while low < high and alist[low] < mid:
            low += 1
        alist[high] = alist[low]  # 此时low指向一个比基准元素大的元素,将low指向的元素放到high空着的位置上,此时low指向的位置空着,之后进行下一次循环,将high找到符合条件的元素填到此处

    # 退出循环后,low与high重合,此时所指位置为基准元素的正确位置,左边的元素都比基准元素小,右边的元素都比基准元素大
    alist[low] = mid  # 将基准元素放到该位置,
    # 对基准元素左边的子序列进行快速排序
    quick_sort(alist, start, low - 1)  # start :0  low -1 原基准元素靠左边一位
    # 对基准元素右边的子序列进行快速排序
    quick_sort(alist, low + 1, end)  # low+1 : 原基准元素靠右一位  end: 最后



if __name__ == '__main__':
    alist = [54, 26, 93, 17, 77, 31, 44, 55, 20]
    quick_sort(alist, 0, len(alist) - 1)
    print(alist)

time complexity

Optimal time complexity: O(nlogn)
Worst time complexity: O(n2)
Stability: Unstable

Guess you like

Origin blog.csdn.net/yjh_SE007/article/details/105662482