blob: b57466d1be7bcfe4738132e45d1c8ab6d069c920 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
def merge(a,b):
c = []
while len(a) != 0 and len(b) != 0:
if a[0] < b[0]:
c.append(a[0])
a.remove(a[0])
else:
c.append(b[0])
b.remove(b[0])
if len(a) == 0:
c += b
else:
c += a
return c
def merge_sort(x):
if len(x) == 0 or len(x) == 1:
return x
else:
middle = len(x)//2
a = merge_sort(x[:middle])
b = merge_sort(x[middle:])
return merge(a,b)
|