-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_sort_sol.py
More file actions
41 lines (35 loc) · 976 Bytes
/
merge_sort_sol.py
File metadata and controls
41 lines (35 loc) · 976 Bytes
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
#merge sort solution
def mergeSort(some_list):
print("Splitting ",some_list)
if len(some_list)>1:
mid = len(some_list)//2
lefthalf = some_list[:mid]
righthalf = some_list[mid:]
print "left side"
mergeSort(lefthalf)
print "right side"
mergeSort(righthalf)
i=0
j=0
k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
some_list[k]=lefthalf[i]
i=i+1
else:
some_list[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):
some_list[k]=lefthalf[i]
i=i+1
k=k+1
while j < len(righthalf):
some_list[k]=righthalf[j]
j=j+1
k=k+1
print("Merging ",some_list)
return some_list
alist = [4 , 2, 52, 24, 34, 12, 42, 57, 23, 135, 34, 43]
mergeSort(alist)
print(alist)