Skip to content

Instantly share code, notes, and snippets.

@Per48edjes
Created January 6, 2018 03:37
Show Gist options
  • Save Per48edjes/6a51e181122867526a3b442a69c4553c to your computer and use it in GitHub Desktop.
Save Per48edjes/6a51e181122867526a3b442a69c4553c to your computer and use it in GitHub Desktop.
Simple Mergesort
#!/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)
@Per48edjes
Copy link
Author

User input functionality only supports a sequence of single digits, although this is easily modifiable as needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment