Created
February 6, 2017 12:08
-
-
Save discort/106458e6b0fc62f2d9f382f4d28949d5 to your computer and use it in GitHub Desktop.
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
INF = float('inf') | |
def merge(A, p, q, r): | |
L = A[p:q + 1] | |
R = A[q + 1:r + 1] | |
L.append(INF) | |
R.append(INF) | |
i = j = 0 | |
for k in range(p, r + 1): | |
if L[i] <= R[j]: | |
A[k] = L[i] | |
i += 1 | |
else: | |
A[k] = R[j] | |
j += 1 | |
def merge_sort(A, p, r): | |
if p < r: | |
q = (p + r) // 2 | |
merge_sort(A, p, q) | |
merge_sort(A, q + 1, r) | |
merge(A, p, q, r) | |
def main(): | |
a = [3, 41, 52, 26, 38, 57, 9, 49, 5] | |
print("source array={}".format(a)) | |
merge_sort(a, 0, len(a) - 1) | |
print("sorted array={0}".format(a)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment