Last active
May 11, 2018 06:36
-
-
Save czheo/2479a99d3e0219e334557a6a6b09a54d to your computer and use it in GitHub Desktop.
merge sort
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
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