Skip to content

Instantly share code, notes, and snippets.

@tiwarinaman
Created August 30, 2020 09:26
Show Gist options
  • Select an option

  • Save tiwarinaman/ae65557866635a86b76bce65d4a83941 to your computer and use it in GitHub Desktop.

Select an option

Save tiwarinaman/ae65557866635a86b76bce65d4a83941 to your computer and use it in GitHub Desktop.
Merge Sort in python
def mearge_sort(list):
if len(list) > 1:
mid = len(list)//2
left_half = list[:mid]
right_half = list[mid:]
mearge_sort(left_half)
mearge_sort(right_half)
i=j=k=0
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
list[k] = left_half[i]
i+=1
else:
list[k] = right_half[j]
j+=1
k+=1
while i<len(left_half):
list[k] = left_half[i]
i+=1
k+=1
while j<len(right_half):
list[k] = right_half[j]
j+=1
k+1
list = [32,21,0,9,5,34]
mearge_sort(list)
print(list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment