-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_sort.py
47 lines (36 loc) · 1.24 KB
/
merge_sort.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
from SortingAlgorithm import SortingAlgorithm
class MergeSort(SortingAlgorithm):
def merge(self, left, right):
i = 0
j = 0
output = []
while i < len(left) and j < len(right):
if left[i] < right[j]:
output.append(left[i])
i = i + 1
else:
output.append(right[j])
j = j + 1
# at this point one of the lists is empty. We can now simply append the
# remaining list to the end of the output. We don't need to check which
# one is empty. We can simply add the emtpy list.
output.extend(left[i:])
output.extend(right[j:])
return output
def merge_sort(self, x):
if len(x) <= 1:
# abort recursion if list has only one element left.
return x
# Divide list in two parts.
left = x[:(len(x)//2)]
right =x [len(x)//2:]
left = self.merge_sort(left)
right = self.merge_sort(right)
return self.merge(left, right)
def sort(self):
self.data = self.merge_sort(self.data)
if __name__ == "__main__":
arr = [4,2,3]
ms = MergeSort(arr)
ms.sort()
print(ms.data)