-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeSort.py
47 lines (38 loc) · 1.48 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
#How to use:
# Initiate an array with any name and call function 'merge_sort' that takes array as an input, start as 1 and end as length of array.
def merge(arr, start, mid1, mid2, end):
left_array = arr[start -1 : mid1]
mid_array = arr[mid1: mid2 + 1]
right_array = arr[mid2 + 1 : end]
left_array.append(float('inf'))
mid_array.append(float('inf'))
right_array.append(float('inf'))
ind_left = 0
ind_mid = 0
ind_right = 0
for i in range(start-1, end):
minimum = min([left_array[ind_left], mid_array[ind_mid], right_array[ind_right]])
if minimum == left_array[ind_left]:
arr[i] = left_array[ind_left]
ind_left += 1
elif minimum == mid_array[ind_mid]:
arr[i] = mid_array[ind_mid]
ind_mid += 1
else:
arr[i] = right_array[ind_right]
ind_right += 1
def merge_sort(arr, start, end):
if len(arr[start -1: end]) < 2:
return arr
else:
mid1 = start + ((end - start) // 3)
mid2 = start + 2 * ((end-start) // 3)
merge_sort(arr, start, mid1)
merge_sort(arr, mid1+1, mid2 + 1)
merge_sort(arr, mid2+2, end)
merge(arr, start, mid1, mid2, end)
return arr
arr = [312,413,3,423,5,3,342,1,2,53]
start = 1 #Start is 1, to comprise with code errors while dividing the array
end = len(arr) #length of array
# print merge_sort(arr, start, end)