-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge.py
More file actions
42 lines (35 loc) · 721 Bytes
/
merge.py
File metadata and controls
42 lines (35 loc) · 721 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
42
"""
gooby = [5,7,3,8,2,4,6,9,1]
[5,7,3,8,2] [4,6,9,1]
[5,7,3] [8,2] [4,6] [9,1]
[5,7] [3] [8] [2] [4][6] [9][1]
[5][7]
[5,7] [3] [8] [2] [4][6] [1][9]
[3,5,7] [2,8] [4,6] [1,9]
[2,3,5,7,8] [1,4,6,9]
[1,2,3,4,5,6,7,8,9]
"""
def merge_sort(alist):
if len(alist) > 1:
mid = len(alist) // 2
left = alist[:mid]
right = alist[mid:]
merge_sort(left)
merge_sort(right)
i=j=k=0
while i < len(left) and j < len(right):
if left[i] < right[j]:
alist[k] = left[i]
i+=1
else:
alist[k] = right[j]
j+=1
k+=1
while i < len(left):
alist[k] = left[i]
i+=1
k+=1
while j < len(right):
alist[k] = right[j]
j+=1
k+=1