1
0
mirror of https://github.com/SunnyQjm/algorithm-review.git synced 2026-06-03 08:16:43 +08:00
Files
algorithm-review/chapter5/8_quick-sort.py
T
2020-06-23 20:42:08 +08:00

30 lines
831 B
Python

#!/usr/bin/env python
# coding=utf-8
###################################################################################
# 快速排序
###################################################################################
def quickSort(A):
def quickSortHelper(A, l, r):
if l >= r:
return
i, j, pivotIndex = l, r, l
while i < j:
while i < j and A[j] >= A[pivotIndex]:
j -= 1
while i < j and A[i] <= A[pivotIndex]:
i += 1
A[i], A[j] = A[j], A[i]
A[i], A[pivotIndex] = A[pivotIndex], A[i]
quickSortHelper(A, l, i - 1)
quickSortHelper(A, i + 1, j)
quickSortHelper(A, 0, len(A) - 1)
if __name__ == '__main__':
A = [5, 7, 1, 3, 6, 2, 4]
quickSort(A)
print(A, "= [1, 2, 3, 4, 5, 6, 7]")