Leetcode Tag Search | Back

Quick sort

Category: /template
import random
def quick_sort(arr):
    def qsort(arr, l, r):
        if l < r:
            p = partition(arr, l, r)
            qsort(arr, l, p - 1)
            qsort(arr, p + 1, r)

    def partition(arr, l, r):
        # simple solution
        # pv = arr[r]

        # this solution avoid almost sorted array 
        # which causes poor performance
        index = random.randint(l, r)
        pv = arr[index]
        arr[index], arr[r] = arr[r], arr[index]

        cur = l
        for i in range(l, r):
            if arr[i] < pv:
                arr[i], arr[cur] = arr[cur], arr[i]
                cur += 1
        arr[cur], arr[r] = arr[r], arr[cur]
        return cur

    qsort(arr, 0, len(arr) - 1)
    return arr

def test_quick_sort():
    data = [1, 3, 2, 5, 4]
    ret = quick_sort(data) 
    # print(ret)
    assert ret == [1, 2, 3, 4, 5]

if __name__ == "__main__":
    """ from terminal, run
    pytest this_file.py
    """
    #import pytest
    #pytest.main()
    test_quick_sort()

讨论

提示

  • 如果看不到讨论部分, 请暂时关掉adblock in Firefox/Chrome
  • 本网站使用Javascript实现评论功能, 此处外链对提高您的网站PR没有帮助. (潜台词: 请不要灌水, 谢谢)