Skip to content

Instantly share code, notes, and snippets.

@devils-ey3
Created September 21, 2016 11:04
Show Gist options
  • Save devils-ey3/2cde1c259769f6e3ced361b1dfe0cb05 to your computer and use it in GitHub Desktop.
Save devils-ey3/2cde1c259769f6e3ced361b1dfe0cb05 to your computer and use it in GitHub Desktop.
Merge Sort By python
def main():
a = []
for i in tuple(map(int,input("Enter the number : ").split(' '))):
a.append(i)
mergesSort(a)
print(a)
def mergesSort(a):
if len(a)>1:
print("================================")
print("Total array {}".format(a))
print("================================")
mid = len(a)//2
leftHalf=a[:mid]
rightHalf=a[mid:]
print("================================")
print("Left Side = ",leftHalf,"\t","Right Side = ",rightHalf)
print("================================")
mergesSort(leftHalf)
mergesSort(rightHalf)
i,j,k = 0,0,0
while i < len(leftHalf) and j < len(rightHalf):
if leftHalf[i]<rightHalf[j]:
a[k]= leftHalf[i]
i+=1
else:
a[k]=rightHalf[j]
j+=1
k+=1
while i < len(leftHalf):
a[k]=leftHalf[i]
k+=1
i+=1
while j < len(rightHalf):
a[k]=rightHalf[j]
k+=1
j+=1
if __name__=="__main__":main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment