#Program to implement Insertion sort algorithm:
n=input("Enter the no. of terms:")
n=int(n)
x=[]
m=0
for i in range (0,n):
m1=input("Enter the no."+str(i+1)+":")
m1=float(m1)
x.append(m1)
for i in range (0,n):
for j in range(i+1,n):
if x[i]>x[j]:
a=x.pop(j)
x.insert(i,a)
m=m+1
print("Sorted elements in ascending order are\n",x,"\nNo. of swapping required:",m)
n=input("Enter the no. of terms:")
n=int(n)
x=[]
m=0
for i in range (0,n):
m1=input("Enter the no."+str(i+1)+":")
m1=float(m1)
x.append(m1)
for i in range (0,n):
for j in range(i+1,n):
if x[i]>x[j]:
a=x.pop(j)
x.insert(i,a)
m=m+1
print("Sorted elements in ascending order are\n",x,"\nNo. of swapping required:",m)
Comments
Post a Comment