Created
May 5, 2021 06:51
-
-
Save freakflames29/a8892c4f7f65fdd3af0677ec68e61dab to your computer and use it in GitHub Desktop.
merge sort in python
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
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