-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.py
More file actions
56 lines (46 loc) · 1.65 KB
/
MergeSort.py
File metadata and controls
56 lines (46 loc) · 1.65 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
"""
Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves,
calls itself for the two halves and then merges the two sorted halves.
The merge() function is used for merging two halves
"""
class MergeSort:
def merge_sort(self, unsorted_list):
if len(unsorted_list) > 1:
mid = len(unsorted_list) // 2
left = unsorted_list[:mid]
right = unsorted_list[mid:]
# Recursive call on each half
self.merge_sort(left)
self.merge_sort(right)
# Two iterators for traversing the two halves
i = 0
j = 0
# Iterator for the main list
k = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
# The value from the left half has been used
unsorted_list[k] = left[i]
# Move the iterator forward
i += 1
else:
unsorted_list[k] = right[j]
j += 1
# Move to the next slot
k += 1
# For all the remaining values from left
while i < len(left):
unsorted_list[k] = left[i]
i += 1
k += 1
# For all the remaining values from right
while j < len(right):
unsorted_list[k] = right[j]
j += 1
k += 1
return unsorted_list
if __name__ == "__main__":
merge_sort_obj = MergeSort()
my_list = [54, 26, 93, 17, 77, 31, 44, 55, 20]
merge_sort_obj.merge_sort(my_list)
print(my_list)