-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort.py
36 lines (32 loc) · 925 Bytes
/
merge_sort.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
# Merge Sort
# O(n log n)
def merge_sort(alist):
if len(alist)>1:
# divide and conquer
mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]
merge_sort(lefthalf)
merge_sort(righthalf)
i=0 # left index pointer
j=0 # right index pointer
k=0 # base index pointer
# aList will be sorted
while i<len(lefthalf) and j<len(righthalf):
if lefthalf[i]<righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]
j=j+1
k=k+1
# merge the left half into aList
while i<len(lefthalf):
alist[k]=lefthalf[i]
i=i+1
k=k+1
# merge the right half into aList
while j<len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1