-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergesort.py
78 lines (66 loc) · 1.86 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
#!/usr/bin/env python
"""Mergesort algorithms and tests"""
"""Austin Hammer"""
"""5.7.2014"""
comp = 0
##############################################
# An algorithm that divides up an array into
# progressively smaller parts, until it is left
# with arrays of only one element. Then it
# recursively merges the lists back together,
# sorting as it goes. It is not an in-place
# algorithm.
def mergesort(array):
if(len(array) <= 1):
return array
left = []
right = []
mid = (int)(len(array) / 2)
for x in range(0, mid):
left.append(array[x])
for x in range(mid, len(array)):
right.append(array[x])
left = mergesort(left)
right = mergesort(right)
return merge(left, right)
##############################################
# The merging part of the algorithm. Comparisons
# are done here. If the algorithm detects one
# list has finished merging, it adds the rest of
# the elements of the other list (which should
# already be sorted) and returns. Equality ties
# go to the B side, not a stable algo
def merge(A, B):
result = []
idxA = 0
idxB = 0
while(idxA < len(A) or idxB < len(B)):
if(idxB == len(B)):
for x in range(idxA, len(A)):
result.append(A[x])
return result
if(idxA == len(A)):
for x in range(idxB, len(B)):
result.append(B[x])
return result
if(A[idxA] <= B[idxB]):
result.append(A[idxA])
idxA+=1
comp_add()
elif(A[idxA] > B[idxB]):
result.append(B[idxB])
idxB+=1
comp_add()
return result;
def comp_add():
global comp
comp += 1
def print_comp():
print(comp)
def main():
array = [4,1,5,9,3,6,7,2,8,0]
newarray = mergesort(array)
print(newarray)
print_comp()
if __name__ == "__main__":
main()