-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathMergeSort.py
More file actions
32 lines (28 loc) · 776 Bytes
/
MergeSort.py
File metadata and controls
32 lines (28 loc) · 776 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
def merge_sort(arr):
if len(arr)>1:
left_arr=arr[:(len(arr)//2)]
right_arr=arr[len(arr)//2:]
merge_sort(left_arr)
merge_sort(right_arr)
i=0
j=0
k=0
while(i<len(left_arr) and j<len(right_arr)):
if(left_arr[i]<right_arr[j]):
arr[k]=left_arr[i]
i+=1
else:
arr[k]=right_arr[j]
j+=1
k+=1
while(i<len(left_arr)):
arr[k]=left_arr[i]
i+=1
k+=1
while(j<len(right_arr)):
arr[k]=right_arr[j];
j+=1
k+=1
array=[5,10,3,4,9,4,7]
merge_sort(array)
print(array)