Created
January 22, 2017 19:17
-
-
Save gquittet/a367b5b7b628dcf822dff427472f8a66 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
import math | |
def merge_sort(t): | |
if len(t) <= 1: | |
return t | |
else: | |
t1, t2 = split(t) | |
t1 = merge_sort(t1) | |
t2 = merge_sort(t2) | |
return merge(t1, t2) | |
def split(t): | |
if len(t) <= 1: | |
return t | |
else: | |
mid = math.floor(len(t) / 2) | |
t1 = t[:mid] | |
t2 = t[mid:] | |
return t1, t2 | |
def merge(t1, t2): | |
if len(t1) == 0: | |
return t2 | |
elif len(t2) == 0: | |
return t1 | |
elif t1[0] > t2[0]: | |
return [t2[0]] + merge(t1, t2[1:]) | |
else: | |
return [t1[0]] + merge(t1[1:], t2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment