-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort.py
36 lines (35 loc) · 906 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
def mergeSort(arr):
if len(arr)>1:
r=len(arr)//2
leftArr = arr[:r]
rightArr = arr[r:]
mergeSort(leftArr)
mergeSort(rightArr)
i=j=k=0
while i<len(leftArr) and j<len(rightArr):
if leftArr[i]<rightArr[j]:
arr[k]=leftArr[i]
i+=1
else:
arr[k] = rightArr[j]
j+=1
k+=1
while i<len(leftArr):
arr[k] = leftArr[i]
i+=1
k+=1
while j<len(rightArr):
arr[k]=rightArr[j]
j+=1
k+=1
def display(arr):
for i in range(len(arr)):
print(arr[i],end='')
print()
if __name__ == '__main__':
arr = [6,5,12,10,9]
print('Original array')
display(arr)
mergeSort(arr)
print('Sorted array')
display(arr)