-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_sort.py
More file actions
62 lines (47 loc) · 1.8 KB
/
merge_sort.py
File metadata and controls
62 lines (47 loc) · 1.8 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
58
59
60
61
62
from typing import List
def merge_sort(array: List):
"""Implement merge sort.
1. Cut the list in half recursively till only one element is left. (dive)
2. Compare the left and the right, and merge them in the ascending order recursively (conquer)
This method is also known as dive-and-conquer
The time complexity is n*log(n). n is the number of the elements in the list.
The space complexity is O(n) as the deepest stack is n
:param array: [description]
:return [description]
"""
if len(array) <= 1:
return array
# Cut in half recursively
middle = len(array) // 2
left = merge_sort(array[0:middle])
right = merge_sort(array[middle: len(array)])
# Sort and merge
sorted_array = merge_array(left, right)
return sorted_array
def merge_array(left: List, right: List) -> List:
"""Merge the left and right lists in the ascending order.
:param left: [description]
:param right: [description]
:return sorted list
"""
sorted_array = []
left_pointer = right_pointer = 0
while (left_pointer < len(left) and right_pointer < len(right)):
# Make comparison between the elements in the two sorted lists
if left[left_pointer] <= right[right_pointer]:
sorted_array.append(left[left_pointer])
left_pointer += 1
else:
sorted_array.append(right[right_pointer])
right_pointer += 1
# Add the rest to the list
# The magic here is the slice as it won't cause error of the index out of range
sorted_array.extend(left[left_pointer:])
sorted_array.extend(right[right_pointer:])
return sorted_array
def main():
array = [3, 5, 8, 7, 1, 6, 2]
sorted_array = merge_sort(array)
print(sorted_array)
if __name__ == "__main__":
main()