-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.py
More file actions
32 lines (27 loc) · 956 Bytes
/
quick_sort.py
File metadata and controls
32 lines (27 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from typing import List
def quick_sort(input_arr: List[int]):
if not input_arr or len(input_arr) == 1:
return
n = len(input_arr)
def _quick_sort(begin: int, end: int):
nonlocal input_arr
if begin >= end:
return
left, right = begin, end
pivot = left + (right - left) // 2
while left < right:
while left < right and input_arr[left] < input_arr[pivot]:
left += 1
while left < right and input_arr[right] > input_arr[pivot]:
right -= 1
if left < right:
input_arr[left], input_arr[right] = input_arr[right], input_arr[left]
if input_arr[left] == input_arr[pivot]:
left += 1
_quick_sort(begin, left-1)
_quick_sort(left+1, end)
_quick_sort(0, n-1)
if __name__ == '__main__':
a = [1, 3, 2, 7, 5, 15, 12]
quick_sort(a)
print(f'A = {a}')