Created
January 6, 2018 03:37
-
-
Save Per48edjes/6a51e181122867526a3b442a69c4553c to your computer and use it in GitHub Desktop.
Simple Mergesort
This file contains hidden or 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
#!/usr/bin/python | |
def merge(l): | |
# Base case | |
if len(l) < 2: | |
return l | |
mid = len(l) // 2 | |
y = merge(l[:mid]) | |
z = merge(l[mid:]) | |
result = [] | |
while (len(y) > 0) and (len(z) > 0): | |
if y[0] > z[0]: | |
result.append(z[0]) | |
z.pop(0) | |
else: | |
result.append(y[0]) | |
y.pop(0) | |
result += y | |
result += z | |
return result | |
if __name__ == "__main__": | |
input_string = input('Type here: ') | |
l = [int(x) for x in input_string] | |
output = merge(l) | |
print(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
User input functionality only supports a sequence of single digits, although this is easily modifiable as needed.