-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmerge_sort.py
52 lines (48 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
48
49
50
51
52
# Date: 2020-08-29
#
# Description:
# Implement merge sort.
#
# Approach:
# Partitions array in equal half and merges them back again in sorted order.
# This is done recursively until all array elements are sorted.
#
# Complexity:
# Time - O(nlogn)
# Space - O(n)
def merge(A, low, mid, high):
first = A[low:mid + 1] # Copy list elements
second = A[mid + 1:high + 1]
i = 0
j = 0
k = low
while i < len(first) and j < len(second):
if first[i] < second[j]:
A[k] = first[i]
i += 1
else:
A[k] = second[j]
j += 1
k += 1
while i < len(first):
A[k] = first[i]
i += 1
k += 1
while j < len(second):
A[k] = second[j]
j += 1
k += 1
def merge_sort(A, low, high):
if low < high:
mid = low + (high - low) // 2
merge_sort(A, low, mid)
merge_sort(A, mid + 1, high)
merge(A, low, mid, high)
def sort(A):
merge_sort(A, 0, len(A) - 1)
return A # To verify assert, otherwise A passed is sorted - inplace sorting
assert sort([3, 4, 5, 2, 1]) == [1, 2, 3, 4, 5]
assert sort([3, 4, 5, 2, 1, 6]) == [1, 2, 3, 4, 5, 6]
assert sort([]) == []
assert sort([1]) == [1]
assert sort([2, 1]) == [1, 2]