Skip to content

Instantly share code, notes, and snippets.

@czheo
Last active May 11, 2018 06:36
Show Gist options
  • Save czheo/2479a99d3e0219e334557a6a6b09a54d to your computer and use it in GitHub Desktop.
Save czheo/2479a99d3e0219e334557a6a6b09a54d to your computer and use it in GitHub Desktop.
merge sort
def merge(left, right, a):
i = j = k = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
# min(left) < min(right)
a[k] = left[i]
i += 1
else:
# min(left) >= min(right)
a[k] = right[j]
j += 1
k += 1
# put the rest elements in "left" into a
while i < len(left):
a[k] = left[i]
i += 1
k += 1
# put the rest elements in "right" into a
while j < len(right):
a[k] = right[j]
j += 1
k += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment