You are on page 1of 2

rsort18 - Jupyter Notebook http://localhost:8888/notebooks/rsort18.

cpp

In [1]: # Write a Python program to store first year percentage of students in arra
# function for sorting array of floating point numbers in ascending order u
# a) Selection Sort
# b) Bubble sort
# and display top five scores

def selectsort(list1):
for i in range(0,len(list1)):
min=list1[i]
index=i
for j in range(i+1,len(list1)):
if (list1[j]<min):
min=list1[j]
index=j
list1[i],list1[index]=list1[index],list1[i]
return list1

def bubblesort(list1):
for i in range(0,len(list1)):
for j in range(0,len(list1)-1):
if list1[j]>list1[j+1]:
list1[j],list1[j+1]=list1[j+1],list1[j]
return list1

#main function
n=int(input("Enter number of students:\n"))
marks=[]
print("Enter marks of students in percentage:")
for i in range(n):
m=float(input())
marks.append(m)
print(marks)
while True:
print("Menu:\n1.selection sort\n2.Bubble sort\n3.exit")
ch=int(input("Enter a choice:\n"))
if(ch==1):
print("Sorted list is: ",selectsort(marks))

if(ch==2):
print("Sorted list is: ",bubblesort(marks))
sorted2=bubblesort(marks)
print("Top five scores are:")
for i in range(1,6):
print(sorted2[-i])

if(ch==3):
print("Thank you for using program.")
break

Enter number of students:


6
Enter marks of students in percentage:
95
75

1 of 2 20/10/23, 11:02
rsort18 - Jupyter Notebook http://localhost:8888/notebooks/rsort18.cpp

69
85
35
72
[95.0, 75.0, 69.0, 85.0, 35.0, 72.0]
Menu:
1.selection sort
2.Bubble sort
3.exit
Enter a choice:
2
Sorted list is: [35.0, 69.0, 72.0, 75.0, 85.0, 95.0]
Top five scores are:
95.0
85.0
75.0
72.0
69.0
Menu:
1.selection sort
2.Bubble sort
3.exit
Enter a choice:
1
Sorted list is: [35.0, 69.0, 72.0, 75.0, 85.0, 95.0]
Menu:
1.selection sort
2.Bubble sort
3.exit
Enter a choice:
3
Thank you for using program.

In [ ]:

In [ ]:

In [ ]:

In [ ]:

2 of 2 20/10/23, 11:02

You might also like