forked from Jack-Lee-Hiter/AlgorithmsByPython
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.py
More file actions
Latest commit
31 lines (27 loc) · 766 Bytes
/
Copy pathMergeSort.py
File metadata and controls
31 lines (27 loc) · 766 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
defmergeSort(alist):
iflen(alist) >1:
mid=len(alist)//2
lefthalf=alist[:mid]
righthalf=alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=0; j=0; k=0
whilei<len(lefthalf) andj<len(righthalf):
iflefthalf[i] <righthalf[j]:
alist[k] =lefthalf[i]
i+=1
else:
alist[k] =righthalf[j]
j+=1
k+=1
whilei<len(lefthalf):
alist[k] =lefthalf[i]
i+=1
k+=1
whilej<len(righthalf):
alist[k] =righthalf[j]
j+=1
k+=1
alist= [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)