-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergesort.py
76 lines (66 loc) · 2.33 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import random
from clip import Clip
class MergeSortClip(Clip):
def __init__(self, N, path):
self.bw = 40
super(MergeSortClip, self).__init__(N*self.bw+40, 60, path, 4)
self.data = list(range(N))
self.pos = (0,0,0,0)
def render(self):
self.canvas.clear()
def box_color(i, text):
if i < self.pos[0]:
return 0xffffff
elif i < self.pos[1]:
return 0xffaaaa
elif i < self.pos[2]:
return 0xaaffaa
elif i < self.pos[3]:
return 0xaaaaff
bbox = self.canvas.text_box_list(20, 30, self.bw, 24, self.data, 'h', box_color=box_color)
self.canvas.text_align_to(bbox, 'merge sort')
def run(self):
data = self.data
random.shuffle(data)
def dump(s, p1, p2, e):
self.pos = (s, p1, p2, e)
self.step()
def merge(arr, start, mid, end):
os = start
start2 = mid + 1
dump(s=os, p1=start, p2=start2, e=end)
if arr[mid] <= arr[start2]:
dump(s=os, p1=end+1, p2=end+1, e=end)
return
while start <= mid and start2 <= end:
# If element 1 is in right place
if arr[start] <= arr[start2]:
start += 1
else:
value = arr[start2]
index = start2
# Shift all the elements between element 1
# element 2, right by 1.
while index != start:
arr[index] = arr[index - 1]
index -= 1
arr[start] = value
start += 1
mid += 1
start2 += 1
dump(s=os, p1=start, p2=start2, e=end)
dump(s=os, p1=end+1, p2=end+1, e=end)
def mergeSort(arr, l, r):
m = l + (r - l) // 2
# Sort first and second halves
if l < m:
mergeSort(arr, l, m)
if m+1 < r:
mergeSort(arr, m + 1, r)
merge(arr, l, m, r)
mergeSort(data, 0, len(data)-1)
for i in range(10):
self.step()
self.finish()
if __name__ == "__main__":
MergeSortClip(20, 'mergesort.mp4').run()