-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge-sort.py
44 lines (40 loc) · 898 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
37
38
39
40
41
42
43
44
def sort(array, m):
mass = []
for i in range(0, m):
mass.append(array[i])
if m <= 1:
array[0] = mass[0]
else:
l = []
r = []
for i in range(0, m):
if i < int(m / 2):
l.append(mass[i])
else:
r.append(mass[i])
sort(l, len(l))
sort(r, len(r))
h = 0
f = 0
c = []
while h < len(l) and f < len(r):
if l[h] < r[f]:
c.append(l[h])
h += 1
else:
c.append(r[f])
f += 1
while h < len(l):
c.append(l[h])
h += 1
while f < len(r):
c.append(r[f])
f += 1
for i in range(0, m):
array[i] = c[i]
a = []
s = input()
for i in s.split(' '):
a.append(int(i))
sort(a, len(a))
print(a)