Skip to content

Instantly share code, notes, and snippets.

@freakflames29
Created May 5, 2021 06:51
Show Gist options
  • Save freakflames29/a8892c4f7f65fdd3af0677ec68e61dab to your computer and use it in GitHub Desktop.
Save freakflames29/a8892c4f7f65fdd3af0677ec68e61dab to your computer and use it in GitHub Desktop.
merge sort in python
def ms(n):
if len(n)>1:
mid=len(n)//2
lefthalf=n[:mid]
righthalf=n[mid:]
ms(lefthalf)
ms(righthalf)
i=j=k=0
while i<len(lefthalf) and j<len(righthalf):
if lefthalf[i]<righthalf[j]:
n[k]=lefthalf[i]
i+=1
k+=1
else:
n[k]=righthalf[j]
j+=1
k+=1
while i<len(lefthalf):
n[k]=lefthalf[i]
i+=1
k+=1
while j<len(righthalf):
n[k]=righthalf[j]
j+=1
k+=1
return n
a=[9,1,5,3,4,3]
print(ms(a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment