-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.py
63 lines (44 loc) · 1.77 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
62
63
import time
def partition(data, head, tail, drawData, timeTick):
border = head
pivot = data[tail]
drawData(data, colorArray(len(data), head, tail, border, border))
time.sleep(timeTick)
for j in range(head, tail):
if(data[j] < pivot):
drawData(data, colorArray(len(data), head, tail, border, j, True))
time.sleep(timeTick)
data[border], data[j] = data[j], data[border]
border +=1
drawData(data, colorArray(len(data), head, tail, border, j))
time.sleep(timeTick)
#swapping pivot element by border value
drawData(data, colorArray(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 colorArray(dataLen, head, tail, border, currIdx, isSwapping = False):
colorArray = []
for i in range(dataLen):
#color when no operation is performed or base coloring
if i >= head and i <= tail:
colorArray.append("gray")
else:
colorArray.append("white")
if i == tail:
colorArray[i] == 'orange'
elif i == border:
colorArray[i] == 'red'
elif i == currIdx:
colorArray[i] == 'yellow'
if isSwapping:
if i == border or i == currIdx:
colorArray[i] = 'green'
return colorArray