forked from dan-sazonov/sirius-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask9_4.py
36 lines (28 loc) · 854 Bytes
/
task9_4.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
n = int(input())
arr = list(map(int, input().split()))
def merge(left, right):
res = []
count = 0
l_index, r_index = 0, 0
while count < len(left) + len(right):
if l_index < len(left) and r_index < len(right):
if left[l_index] <= right[r_index]:
res.append(left[l_index])
l_index += 1
else:
res.append(right[r_index])
r_index += 1
elif l_index == len(left):
res.append(right[r_index])
r_index += 1
elif r_index == len(right):
res.append(left[l_index])
l_index += 1
count += 1
return res
def m_s(nums):
half = len(nums) // 2
if len(nums) > 1:
return merge(m_s(nums[:half]), m_s(nums[half:]))
return nums
print(' '.join(map(str, m_s(arr))))