forked from iiitv/algos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_sort.py
53 lines (47 loc) · 1.28 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
48
49
50
51
52
53
def merge(array, left, right):
"""
Perform Merge Operation between arrays.
Time Complexity: Theta(nLogn)
Auxiliary Space: O(n)
:param array: Iterable of elements
:param left: left limit for merge sort
:param right: right limit for merge sort
:return: no returns, merges arrays.
"""
mid = (left + right) // 2
l = array[left:mid + 1]
r = array[mid + 1:right + 1]
k = left
while l and r:
if l[0] < r[0]:
array[k] = l.pop(0)
else:
array[k] = r.pop(0)
k += 1
while l:
array[k] = l.pop(0)
k += 1
while r:
array[k] = r.pop(0)
k += 1
def merge_sort(array, left, right):
"""
Perform sort using merge function.
Time Complexity : O(nlog(n)).
Space Complexity : O(n).
:param array: Iterable of elements
:param left: left limit for merge sort
:param right: right limit for merge sort
:return: no returns, sorts array
"""
if left < right:
mid = (left + right) // 2
merge_sort(array, left, mid)
merge_sort(array, mid + 1, right)
merge(array, left, right)
def main():
a = [15, 19, 18, 26, 456, 87, 45, -1, 558897984]
merge_sort(a, 0, len(a) - 1)
print(a)
if __name__ == '__main__':
main()