def merge(arr, l, m, r):
n1 = m - l + 1
n2 = r- m
L = [0] * (n1)
R = [0] * (n2)
print(l,m)
# Copy data to temp arrays L[] and R[]
for i in range(0 , n1):
L[i] = arr[l + i]
for j in range(0 , n2):
R[j] = arr[m + 1 + j]
i = 0 # Initial index of first subarray
j = 0 # Initial index of second subarray
k = l # Initial index of merged subarray
while i < n1 and j < n2 :
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
# Copy the remaining elements of L[], if there
# are any
while i < n1:
arr[k] = L[i]
i += 1
k += 1
# Copy the remaining elements of R[], if there
# are any
while j < n2:
arr[k] = R[j]
j += 1
k += 1
def mergeSort(arr,l,r):
if l < r:
m = int((l+(r-1))/2)
mergeSort(arr, l, m)
mergeSort(arr, m+1, r)
merge(arr, l, m, r)
arr = [12, 11, 13, 5, 6, 7]
n = len(arr)
print ("Given array is")
for i in range(n):
print ("%d" %arr[i]),
mergeSort(arr,0,n-1)
print ("\n\nSorted array is")
for i in range(n):
print ("%d" %arr[i]),
ZGVmIG1lcmdlKGFyciwgbCwgbSwgcik6IAogICAgbjEgPSBtIC0gbCArIDEKICAgIG4yID0gci0gbSAKCiAgICBMID0gWzBdICogKG4xKSAKICAgIFIgPSBbMF0gKiAobjIpIAogICAgcHJpbnQobCxtKQogICAgIyBDb3B5IGRhdGEgdG8gdGVtcCBhcnJheXMgTFtdIGFuZCBSW10gCiAgICBmb3IgaSBpbiByYW5nZSgwICwgbjEpOiAKICAgICAgICBMW2ldID0gYXJyW2wgKyBpXSAKICAKICAgIGZvciBqIGluIHJhbmdlKDAgLCBuMik6IAogICAgICAgIFJbal0gPSBhcnJbbSArIDEgKyBqXSAKICAKICAgIGkgPSAwICAgICAjIEluaXRpYWwgaW5kZXggb2YgZmlyc3Qgc3ViYXJyYXkgCiAgICBqID0gMCAgICAgIyBJbml0aWFsIGluZGV4IG9mIHNlY29uZCBzdWJhcnJheSAKICAgIGsgPSBsICAgICAjIEluaXRpYWwgaW5kZXggb2YgbWVyZ2VkIHN1YmFycmF5IAogIAogICAgd2hpbGUgaSA8IG4xIGFuZCBqIDwgbjIgOiAKICAgICAgICBpZiBMW2ldIDw9IFJbal06IAogICAgICAgICAgICBhcnJba10gPSBMW2ldIAogICAgICAgICAgICBpICs9IDEKICAgICAgICBlbHNlOiAKICAgICAgICAgICAgYXJyW2tdID0gUltqXSAKICAgICAgICAgICAgaiArPSAxCiAgICAgICAgayArPSAxCiAgCiAgICAjIENvcHkgdGhlIHJlbWFpbmluZyBlbGVtZW50cyBvZiBMW10sIGlmIHRoZXJlIAogICAgIyBhcmUgYW55IAogICAgd2hpbGUgaSA8IG4xOiAKICAgICAgICBhcnJba10gPSBMW2ldIAogICAgICAgIGkgKz0gMQogICAgICAgIGsgKz0gMQogIAogICAgIyBDb3B5IHRoZSByZW1haW5pbmcgZWxlbWVudHMgb2YgUltdLCBpZiB0aGVyZSAKICAgICMgYXJlIGFueSAKICAgIHdoaWxlIGogPCBuMjogCiAgICAgICAgYXJyW2tdID0gUltqXSAKICAgICAgICBqICs9IDEKICAgICAgICBrICs9IDEKICAKCmRlZiBtZXJnZVNvcnQoYXJyLGwscik6IAogICAgaWYgbCA8IHI6IAogICAgICAgIG0gPSBpbnQoKGwrKHItMSkpLzIpCiAgICAgICAgbWVyZ2VTb3J0KGFyciwgbCwgbSkgCiAgICAgICAgbWVyZ2VTb3J0KGFyciwgbSsxLCByKSAKICAgIAltZXJnZShhcnIsIGwsIG0sIHIpIAogIAogIAphcnIgPSBbMTIsIDExLCAxMywgNSwgNiwgN10gCm4gPSBsZW4oYXJyKSAKcHJpbnQgKCJHaXZlbiBhcnJheSBpcyIpIApmb3IgaSBpbiByYW5nZShuKTogCiAgICBwcmludCAoIiVkIiAlYXJyW2ldKSwgCiAgCm1lcmdlU29ydChhcnIsMCxuLTEpIApwcmludCAoIlxuXG5Tb3J0ZWQgYXJyYXkgaXMiKSAKZm9yIGkgaW4gcmFuZ2Uobik6IAogICAgcHJpbnQgKCIlZCIgJWFycltpXSksIA==