forked from sat5297/hacktober-coding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergesort.py
39 lines (30 loc) · 847 Bytes
/
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
# Python program for implementation of
# MergeSort (Alternative)
def merge_sort(values):
if len(values)>1:
m = len(values)//2
left = values[:m]
right = values[m:]
left = merge_sort(left)
right = merge_sort(right)
values =[]
while len(left)>0 and len(right)>0:
if left[0]<right[0]:
values.append(left[0])
left.pop(0)
else:
values.append(right[0])
right.pop(0)
for i in left:
values.append(i)
for i in right:
values.append(i)
return values
# Input list
a = [12, 11, 13, 5, 6, 7]
print("Given array is")
print(*a)
a = merge_sort(a)
# Print output
print("Sorted array is : ")
print(*a)