-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmergesort_code.py
38 lines (31 loc) · 1.1 KB
/
mergesort_code.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
import time
def merge(data, start, mid, end, drawData, delay):
p = start
q = mid + 1
tempArray = []
for i in range(start, end+1):
if p > mid:
tempArray.append(data[q])
q+=1
elif q > end:
tempArray.append(data[p])
p+=1
elif data[p] < data[q]:
tempArray.append(data[p])
p+=1
else:
tempArray.append(data[q])
q+=1
for p in range(len(tempArray)):
data[start] = tempArray[p]
start += 1
def merge_sort(data, start, end, drawData, delay):
if start < end:
mid = int((start + end) / 2)
merge_sort(data, start, mid, drawData, delay)
merge_sort(data, mid+1, end, drawData, delay)
merge(data, start, mid, end, drawData, delay)
drawData(data, ["PURPLE" if x >= start and x < mid else "YELLOW" if x == mid
else "pink" if x > mid and x <=end else 'blue' for x in range(len(data))])
time.sleep(delay)
drawData(data, ['blue' for x in range(len(data))])