forked from ZQPei/Sorting_Visualization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergesort.py
More file actions
57 lines (44 loc) · 1.14 KB
/
Copy pathmergesort.py
File metadata and controls
57 lines (44 loc) · 1.14 KB
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
import copy
from data import DataSeq
def Merge(ds, L, R, RightEnd, time_interval):
tmpData = copy.copy(ds.data)
LeftEnd = R-1
i=L
j=R
k=L
# import ipdb; ipdb.set_trace()
while i<=LeftEnd and j<=RightEnd:
if tmpData[i] < tmpData[j]:
ds.SetVal(k, tmpData[i])
i+=1
else:
ds.SetVal(k, tmpData[j])
j+=1
k+=1
while i<=LeftEnd:
ds.SetVal(k, tmpData[i])
k+=1
i+=1
while j<=RightEnd:
ds.SetVal(k, tmpData[j])
k+=1
j+=1
def Sort(ds, L, RightEnd, time_interval):
# import ipdb; ipdb.set_trace()
if RightEnd>L:
mid = (L+RightEnd)//2
Sort(ds,L,mid, time_interval)
Sort(ds,mid+1,RightEnd, time_interval)
Merge(ds,L,mid+1,RightEnd, time_interval)
def MergeSort(ds, time_interval=1):
assert isinstance(ds, DataSeq), "Type Error"
Length = ds.length
Sort(ds, 0,Length-1, time_interval)
if __name__ == "__main__":
ds=DataSeq(64)
ds.Visualize()
ds.StartTimer()
MergeSort(ds)
ds.StopTimer()
ds.SetTimeInterval(0)
ds.Visualize()