-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeSort.py
67 lines (54 loc) · 1.71 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
# Merge Sort divides list into 2 halves until cant divide
# then merges the laves by sorting them
# TC: O(NLogN)
# SC: O(N)
# takes first index , middle and last index
def merge(custom_list, first, middle, last):
# first subarray and second subarray elems
num1 = middle - first + 1
num2 = last - middle
# right and left subrarrays
left_temp = [0] * (num1)
right_temp = [0] * (num2)
# copy elements from input list to subarrays
for i in range(0, num1):
left_temp[i] = custom_list[first+i]
for j in range(0, num2):
right_temp[j] = custom_list[middle+1+j]
# first index of first subarray
i = 0
# first index of second subarray
j = 0
# first index of merged subarray
k = first
# Merging with sorted order
while i < num1 and j < num2:
if left_temp[i] <= right_temp[j]:
custom_list[k] = left_temp[i]
i += 1
else:
custom_list[k] = right_temp[j]
j += 1
k += 1
# copy elements of left sarray, if there are any
while i < num1:
custom_list[k] = left_temp[i]
i += 1
k += 1
# copy elements of right sarray, if there are any
while j < num2:
custom_list[k] = right_temp[j]
j += 1
k += 1
# first index and last index of list
def mergeSort(custom_list, first, last):
if first < last:
middle = (first+(last-1))//2
# These calls will be recursive
# merge sort for first part
mergeSort(custom_list, first, middle)
# second part
mergeSort(custom_list, middle+1, last)
# divide and merge the parts
merge(custom_list, first, middle, last)
return custom_list