-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergesort.py
54 lines (42 loc) · 1.67 KB
/
mergesort.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
import time
def merge_sort(data, drawData, timeTick):
merge_sort_alg(data,0, len(data)-1, drawData, timeTick)
def merge_sort_alg(data, left, right, drawData, timeTick):
if left < right:
middle = (left + right) // 2
merge_sort_alg(data, left, middle, drawData, timeTick)
merge_sort_alg(data, middle+1, right, drawData, timeTick)
merge(data, left, middle, right, drawData, timeTick)
def merge(data, left, middle, right, drawData, timeTick):
drawData(data, getColorArray(len(data), left, middle, right))
time.sleep(timeTick)
leftPart = data[left:middle+1]
rightPart = data[middle+1: right+1]
leftIdx = rightIdx = 0
for dataIdx in range(left, right+1):
if leftIdx < len(leftPart) and rightIdx < len(rightPart):
if leftPart[leftIdx] <= rightPart[rightIdx]:
data[dataIdx] = leftPart[leftIdx]
leftIdx += 1
else:
data[dataIdx] = rightPart[rightIdx]
rightIdx += 1
elif leftIdx < len(leftPart):
data[dataIdx] = leftPart[leftIdx]
leftIdx += 1
else:
data[dataIdx] = rightPart[rightIdx]
rightIdx += 1
drawData(data, ["green" if x >= left and x <= right else "white" for x in range(len(data))])
time.sleep(timeTick)
def getColorArray(leght, left, middle, right):
colorArray = []
for i in range(leght):
if i >= left and i <= right:
if i >= left and i <= middle:
colorArray.append("yellow")
else:
colorArray.append("pink")
else:
colorArray.append("white")
return colorArray