-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmerge_sort.py
58 lines (46 loc) · 1.5 KB
/
merge_sort.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
# In merge sort our approach is to divide the array in 2 equal half and recursively sort them and finally merge them again
def MergeSort(a):
# Base case for recusion
if len(a) == 0 or len(a) == 1:
return
# finding middle to divide the array in equal half
mid = len(a) // 2
# seprating given array into 2 array
a1 = a[:mid]
a2 = a[mid:]
# calling mergesort on them as our recursion hypothesis is that it will give us 2 sorted array
MergeSort(a1)
MergeSort(a2)
# merging both sorted array into one
merge(a1, a2, a)
# for merging 2 sroted array we use 2 pointers which iterate on both of them,compare the elements and add them into final array
def merge(a1, a2, a):
# here i,j are pointers which will iterate over array and help us to compare the elements
i = 0
j = 0
# we take k as a pointer to add element in main array
k = 0
# comparing elements from both array and adding it to main array
while i < len(a1) and j < len(a2):
if a1[i] < a2[j]:
a[k] = a1[i]
k = k + 1
i = i + 1
else:
a[k] = a2[j]
k = k + 1
j = j + 1
# adding rest of elements left in array1
while i < len(a1):
a[k] = a1[i]
k = k + 1
i = i + 1
# adding rest of elements left in array2
while j < len(a2):
a[k] = a2[j]
k = k + 1
j = j + 1
# Main
arr = list(int(i) for i in input().strip().split(' '))
MergeSort(arr)
print(arr)