Python 快速排序
阿新 • • 發佈:2018-03-04
quic 復雜度 快速 nlog 快速排序 return 情況 最壞情況 code
最好情況:時間復雜度 O(nlog2n)
最壞情況:逆序序列,時間復雜度為O(n2)
平均時間復雜度:O(nlogn)
空間復雜度:O(nlog2n)
穩定性:不穩定
array_test = [5, 9, 10, 6, 5, 26, 17, 4, 11, 8]
def quick_sort(array, low, high):
if low >= high:
return
key = array[low]
start, end = low, high
while low < high:
while low < high and array[high] >= key:
high -= 1
if low < high:
array[low] = array[high]
low += 1
while low < high and array[low] <= key:
low += 1
if low < high:
array[high] = array[low]
high -= 1
array[low] = key
quick_sort(array, start, low - 1)
quick_sort(array, high + 1, end)
return array
print(quick_sort(array_test, 0, len(array_test) - 1))
輸出:[4, 5, 5, 6, 8, 9, 10, 11, 17, 26]
Python 快速排序