-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_sort.py
More file actions
59 lines (41 loc) · 1.13 KB
/
merge_sort.py
File metadata and controls
59 lines (41 loc) · 1.13 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
A = [4, 9, 11, 8, 7, 6, 2, 1, 12]
"""unsuccessful attempt using
pseudocde from slide"""
# low = 0
# high = len(A)
# def Merge_sort(A, low, high):
# if low < high:
# mid=(low+high)//2
# Merge_sort(A, low, mid)
# Merge_sort(A, mid+1, high)
# Merge(A, low, mid, high)
# #mid=q, low = p, high = r
# def Merge(A, low, mid, high):
# n1=mid-low+1
# n2=high-mid
# K=[i for i in range(low,n1+1)]
def Merge_sort(A):
if len(A) <= 1:
return A
Left = [A[i] for i in range(0,len(A)//2)]
Right = [A[i] for i in range(len(A)//2,len(A))]
Left = Merge_sort(Left)
Right = Merge_sort(Right)
return Merge(Left, Right)
def Merge(Left, Right):
New = []
while Left and Right:
if Left[0] > Right[0]:
New.append(Right[0])
Right.pop(0)
else:
New.append(Left[0])
Left.pop(0)
while Left:
New.append(Left[0])
Left.pop(0)
while Right:
New.append(Right[0])
Right.pop(0)
return New
print(Merge_sort(A))