-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickSort.py
61 lines (44 loc) · 1.67 KB
/
quickSort.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import time
def partition(data, head, tail, drawData, timeTick):
border = head
pivot = data[tail]
drawData(data, getColorArray(len(data), head, tail, border, border))
time.sleep(timeTick)
for j in range(head, tail):
if data[j] < pivot:
drawData(data, getColorArray(len(data), head, tail, border, j, True))
time.sleep(timeTick)
data[border], data[j] = data[j], data[border]
border += 1
drawData(data, getColorArray(len(data), head, tail, border, j))
time.sleep(timeTick)
# swap pivot with border value
drawData(data, getColorArray(len(data), head, tail, border, tail, True))
time.sleep(timeTick)
data[border], data[tail] = data[tail], data[border]
return border
def quick_sort(data, head, tail, drawData, timeTick):
if head < tail:
partitionIdx = partition(data, head, tail, drawData, timeTick)
# LEFT PARTITION
quick_sort(data, head, partitionIdx - 1, drawData, timeTick)
# RIGHT PARTITION
quick_sort(data, partitionIdx + 1, tail, drawData, timeTick)
def getColorArray(dataLen, head, tail, border, currIdx, isSwaping=False):
colorArray = []
for i in range(dataLen):
# base coloring
if i >= head and i <= tail:
colorArray.append('gray')
else:
colorArray.append('white')
if i == tail:
colorArray[i] = 'blue'
elif i == border:
colorArray[i] = 'red'
elif i == currIdx:
colorArray[i] = 'yellow'
if isSwaping:
if i == border or i == currIdx:
colorArray[i] = 'green'
return colorArray