Skip to content

Instantly share code, notes, and snippets.

@pysoftware
Created March 10, 2019 15:27
Show Gist options
  • Save pysoftware/d298f395b6ff13e5abbd4271a9be67ed to your computer and use it in GitHub Desktop.
Save pysoftware/d298f395b6ff13e5abbd4271a9be67ed to your computer and use it in GitHub Desktop.
Merge sort( РАЗОБРАТЬСЯ! )
# Сортировка слиянием
def merge(a, b):
i, j = 0, 0
n, m = len(a), len(b)
c = []
while i < n or j < m:
if j == m or (i < n and a[i] <= b[j]):
c.append(a[i])
i += 1
else:
c.append(b[j])
j += 1
return c
def merge_sort(a):
n = len(a)
if n == 1:
return a
l = a[:((n//2)-1)]
r = a[(n//2):(n-1)]
l = merge_sort(l)
r = merge_sort(r)
return merge(l, r)
# a = [3,4,5,7]
# b = [3,4,5,0]
# print(merge(sorted(a), sorted(b)))
print(merge_sort([3,4,5,0,1,3,4,5,6,7]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment