-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_sort.py
31 lines (27 loc) · 901 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
def merge(n,m):
print n,m;
n_counter = 0;
m_counter = 0;
J = [];
for i in range(len(n)+len(m)):
if n[n_counter]>=m[m_counter]:
J.append(m[m_counter]);
if m_counter==len(m)-1:
for j in range(n_counter,len(n)):
J.append(n[j]);
return J;
m_counter+=1;
else:
J.append(n[n_counter]);
if n_counter==len(n)-1:
for j in range(m_counter,len(m)):
J.append(m[j]);
return J;
n_counter+=1;
return J;
def merge_sort(n):
if len(n) < 2: return n;
else:
return merge(merge_sort(n[0:int(len(n)/2)]),merge_sort(n[int(len(n)/2):len(n)]));
print merge_sort([10,9,8,7,6,5,3,2,21,4,3,2,1,0]);
print sorted([10,9,8,7,6,5,3,2,21,4,3,2,1,0]);