-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort.py
74 lines (56 loc) · 1.98 KB
/
MergeSort.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def merge(array, left_index, right_index, middle):
left_copy = array[left_index:middle+1]
right_copy = array[middle+1:right_index+1]
i = 0
j = 0
k = i
while i < len(left_copy) and j < len(right_copy):
if left_copy[i] <= right_copy[j]:
array[k] = left_copy[i]
i += 1
else:
array[k] = right_copy[j]
j += 1
k += 1
while i < len(left_copy):
array[k] = left_copy[i]
i += 1
k += 1
while j < len(right_copy):
array[k] = right_copy[j]
j += 1
k += 1
def mergeM(array, left_index, right_index, middle):
left_copy = array[left_index:middle + 1]
right_copy = array[middle+1:right_index+1]
left_copy_index = 0
right_copy_index = 0
sorted_index = left_index
while left_copy_index < len(left_copy) and right_copy_index < len(right_copy):
if left_copy[left_copy_index] <= right_copy[right_copy_index]:
array[sorted_index] = left_copy[left_copy_index]
left_copy_index = left_copy_index + 1
else:
array[sorted_index] = right_copy[right_copy_index]
right_copy_index = right_copy_index + 1
sorted_index = sorted_index + 1
while left_copy_index < len(left_copy):
array[sorted_index] = left_copy[left_copy_index]
left_copy_index = left_copy_index + 1
sorted_index = sorted_index + 1
while right_copy_index < len(right_copy):
array[sorted_index] = right_copy[right_copy_index]
right_copy_index = right_copy_index + 1
sorted_index = sorted_index + 1
def merge_sort(array, l, r):
if l >= r:
return
middle = (l + r) // 2
merge_sort(array, l, middle)
merge_sort(array, middle+1, r)
mergeM(array, l, r, middle)
data = [33, 42, 9, 37, 8, 47, 5, 29, 49, 31, 4, 48, 16, 22, 26]
merge_sort(data, 0, len(data) - 1)
print(data)
print('Expected: ',
[4, 5, 8, 9, 16, 22, 26, 29, 31, 33, 37, 42, 47, 48, 49])