Adriano Laureano
6 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with
27 additions and
0 deletions
-
sorting/merge_sort.py
|
|
@ -0,0 +1,27 @@ |
|
|
|
def merge_sort(a): |
|
|
|
if len(a) < 2: |
|
|
|
return a |
|
|
|
l = a[0:len(a)//2] |
|
|
|
r = a[len(a)//2:] |
|
|
|
return merge(merge_sort(l), merge_sort(r)) |
|
|
|
|
|
|
|
|
|
|
|
def merge(a, b): |
|
|
|
out = [] |
|
|
|
i = 0 |
|
|
|
j = 0 |
|
|
|
while i < len(a) or j < len(b): |
|
|
|
if i >= len(a): |
|
|
|
out.append(b[j]) |
|
|
|
j += 1 |
|
|
|
elif j >= len(b): |
|
|
|
out.append(a[i]) |
|
|
|
i += 1 |
|
|
|
else: |
|
|
|
if a[i] <= b[j]: |
|
|
|
out.append(a[i]) |
|
|
|
i += 1 |
|
|
|
else: |
|
|
|
out.append(b[j]) |
|
|
|
j += 1 |
|
|
|
return out |