Skip to content

Instantly share code, notes, and snippets.

@umangshrestha
Created June 17, 2020 17:23
Show Gist options
  • Save umangshrestha/7d48f7f7e530b0b604586140f1d7124b to your computer and use it in GitHub Desktop.
Save umangshrestha/7d48f7f7e530b0b604586140f1d7124b to your computer and use it in GitHub Desktop.
def mergeSort(array):
if len(array) > 1:
r = len(array)//2
left_half = array[:r]
right_half = array[r:]
i = j = k = 0
mergeSort(right_half)
mergeSort(left_half)
while (i < len(left_half) and j < len(right_half)):
if left_half[i] <= right_half[j]:
print("array", k, left_half[i])
array[k] = left_half[i]
i = i + 1
else:
array[k] = right_half[j]
j = j + 1
k = k + 1
while (i < len(left_half)):
#print("left array", k, left_half[i])
array[k] = left_half[i]
i = i + 1
k = k + 1
while (j < len(right_half)):
# print("right array", k, right_half[j])
array[k] = right_half[j]
j = j + 1
k = k + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment