-
Notifications
You must be signed in to change notification settings - Fork 13
/
MergeSort.py
57 lines (47 loc) · 1.55 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
#helper funtion for merging
def merge(customList, left, mid, right):
#number of elements in 1st subarray
n1 = mid - left + 1
n2 = right - mid
# creating subarray
left_subarray = [0] * n1
right_subarray = [0] * n2
# copying values from customlist to subarrays
for i in range(0,n1):
left_subarray[i] = customList[left + i]
for j in range(0,n2):
right_subarray[j] = customList[mid + 1 + j]
# initial indexes
i = 0 #index of left subarray
j = 0 #index of right subarray
k = left # index of merged list
#loop to perform merging after sorting
while i < n1 and j < n2:
if left_subarray[i] <= right_subarray[j]:
customList[k] = left_subarray[i]
i += 1
else:
customList[k] = right_subarray[j]
j += 1
k += 1
#loop for copying remaining element of left_subarray
while i < n1:
customList[k] = left_subarray[i]
i += 1
k += 1
#loop for copying remaining element of right_subarray
while j < n2:
customList[k] = right_subarray[j]
j += 1
k += 1
#merge sort function
def mergeSort(customList, left, right):#time - O(NlogN), SPACE- O(N)
if left < right:
mid = (left + (right-1))//2
mergeSort(customList, left, mid)
mergeSort(customList, mid+1, right)
merge(customList, left, mid, right)
return customList
#check how it works
cList = [2,1,7,6,5,3,4,9,8]
print(mergeSort(cList, 0, 8))