-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmergesort.py
66 lines (45 loc) · 2.34 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
'''
Merge Sort
Merge sort is a Divide and Conquer based Algorithm. It divides the input array into two-parts, until the size of the input array is not ‘1’. In the return part, it will merge two sorted arrays a return a whole merged sorted array.
This is the recursive approach for implementing merge sort. The steps needed to obtain the sorted array through this method can be found below:
1.The list is divided into left and right in each recursive call until two adjacent elements are obtained.
2.Now begins the sorting process. The i and j iterators traverse the two halves in each call. The k iterator traverses the whole lists and makes changes along the way.
3.If the value at i is smaller than the value at j, left[i] is assigned to the arr[k] slot and i is incremented. If not, then right[j] is chosen.
4.This way, the values being assigned through k are all sorted.
5.At the end of this loop, one of the halves may not have been traversed completely. Its values are simply assigned to the remaining slots in the list.
And with that, the merge sort has been implemented!
Time Complexity - O(nlogn)
'''
def mergesort(arr):
if len(arr)>1:
left = arr[:len(arr)//2]
right = arr[len(arr)//2:]
#recursion
mergesort(left)
mergesort(right)
#merge
i=0 #left array index
j=0 #right array index
k=0 #merged array index
#to look at every element in the right array and transfer every element from the right to the merged array
while i<len(left) and j<len(right):
if left[i] <right[j]:
arr[k] = left[i]
i += 1
#k += 1
else:
arr[k] = right[j]
j += 1
#k += 1
k += 1 #in both cases we need to increment k
#to look at every element in the left array and transfer every element from the left to the merged array
while i<len(left): #because there's element missing from the left array to transfer
arr[k] = left[i]
i += 1
k += 1
while j<len(right): #to check if we are not at the end of array
arr[k] = right[j]
j += 1
k += 1
return arr
print(mergesort([8, 3, 5, 6, 11, 19, 7])) #[3, 5, 6, 7, 8, 11, 19]