Problem Statement: Write a Python program to store second year percentage of students in array. Write function for sorting array of floating point numbers in ascending order using
a) Insertion sort
b) Shell Sort and display top five scores.
Note:- Scroll horizontally to see the full line of code.
def insertion_sort(a,n):
for i in range(1,n):
current=a[i]
j=i-1
while((a[j]>current) and j>=0):
temp=a[j+1]
a[j+1]=a[j]
a[j]=temp
j=j-1
a[j+1]=current
print("Iteration",i,": ",a)
return a
def shell_sort(a,n):
gap=n//2
p=1
while(gap>=1):
for i in range(gap,n):
current=a[i]
j=i-gap
while((a[j]>current) and j>=0):
temp=a[j+gap]
a[j+gap]=a[j]
a[j]=temp
j=j-gap
a[j+gap]=current
gap=gap//2
print("Pass",p,": ",a)
p=p+1
return a
cont='y'
while(cont=='y'):
n=int(input("Enter the no. of students: "))
if(n<5):
while(n<5):
print("Sorry, Number of students can not be less than 5")
n=int(input("Enter the no. of students again: "))
a=[]
for i in range(n):
print("Enter percentage studnet",i,": ",end="")
elem=float(input())
if(elem<=100):
a.append(elem)
else:
while(elem>100):
print("Percentage can not be greater than 100")
print("Enter percentage studnet",i," again: ",end="")
elem=float(input())
a.append(elem)
print("List of percentage of students is: ",a,"\n")
print("<-----Menu----->")
print("1.Insertion sort")
print("2.Shell sort")
choice=int(input("Enter the choice: "))
if(choice==1):
b=insertion_sort(a,n)
print("The sorted list of percentages (by insertion sort) is: ",b)
print("The Top 5 scores are:")
for i in range(n-1,n-6,-1):
print(n-i,"th: ",b[i])
if(choice==2):
c=shell_sort(a,n)
print("The sorted list of percentages (by shell sort) is: ",c)
print("The Top 5 scores are:")
for i in range(n-1,n-6,-1):
print(n-i,"th: ",c[i])
cont=input("Do you want to continue? (y/n): ")
if(cont=='n'):
print("Thank you!!!")
Comments
Post a Comment