-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMergeSort.py
129 lines (93 loc) · 2.73 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#Implementation of MergeSort recurssive and interactive
#with Timers for academic propurses
import random
from time import time
def createList(n):
alist = []
for i in range(n, 0, -1):
alist.append(random.randint(0, n))
return alist
def mergeSort(alist):
#print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=0
j=0
k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1
k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1
#print("Merging ",alist)
def merge_2(seq):
listOfLists = []
for i in range(len(seq)):
temp = [seq[i]]
listOfLists.append(temp)
while (len(listOfLists) != 1):
j = 0
while (j < len(listOfLists) - 1):
tempList = merge(listOfLists[j], listOfLists[j + 1])
listOfLists[j] = tempList
del listOfLists[j + 1]
j = j + 1
def merge(a, b):
newList = []
a1, a2 = 0, 0
while ((a1 < len(a)) and (a2 < len(b))):
if (a[a1] > b[a2]):
newList.append(b[a2])
a2 = a2 + 1
elif (a[a1] < b[a2]):
newList.append(a[a1])
a1 = a1 + 1
elif (a[a1] == b[a2]):
newList.append(a[a1])
newList.append(b[a2])
a1, a2 = a1 + 1, a2 + 1
if (a1 < len(a)):
for f in range(a1, len(a)):
newList.append(a[f])
elif (a2 < len(b)):
for k in range(a2, len(b)):
newList.append(b[k])
return newList
def performace(lenList):
seqOri = createList(lenList)
seq1 = list(seqOri)
seq2 = list(seqOri)
begin = time()
mergeSort(seq1)
end = time()
diff = (end - begin) * 1000
print("Recursive: " + str(diff))
print("seq1: " + str(seq1))
print("")
begin = time()
merge_2(seq2)
end = time()
diff = (end - begin) * 1000
print("interative: " + str(diff))
print("seq2: " + str(seq2))
print("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------")
if __name__ == '__main__':
performace(10)
#performace(500)
#performace(1000)
#performace(10000)