Skip to content

Instantly share code, notes, and snippets.

@nullset2
Created August 16, 2016 18:31
Show Gist options
  • Save nullset2/21bc4717c5f3b0ee9e59f58cac2ae0e8 to your computer and use it in GitHub Desktop.
Save nullset2/21bc4717c5f3b0ee9e59f58cac2ae0e8 to your computer and use it in GitHub Desktop.
def merge(a, b):
i = 0
j = 0
c = []
while(i < len(a) and j < len(b)):
if(a[i] <= b[j]):
c.append(a[i])
i += 1
else:
c.append(b[j])
j += 1
c.extend(a[i:])
c.extend(b[j:])
return c
def merge_sort(array):
array_length = len(array)
if(array_length == 1):
return array
half = array_length // 2
l = merge_sort(array[:half])
r = merge_sort(array[half:])
return merge(l, r)
input_array = map(int, raw_input("Enter an array with space-separated elements: ").strip().split())
print(merge_sort(input_array))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment