-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.py
More file actions
26 lines (23 loc) · 732 Bytes
/
quickSort.py
File metadata and controls
26 lines (23 loc) · 732 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
comparisons = 0
swaps = 0
def quickSort(array, left, right):
global comparisons, swaps
if left >= right:
return
index = left
random_index = right
array[right], array[random_index] = array[random_index], array[right]
swaps += 1
for j in range(left, right):
yield array, j, index, comparisons, swaps
comparisons = 0
swaps = 0
comparisons += 1
if array[j] < array[right]:
swaps += 1
array[j], array[index] = array[index], array[j]
index += 1
array[index], array[right] = array[right], array[index]
swaps += 1
yield from quickSort(array, index + 1, right)
yield from quickSort(array, left, index - 1)