Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Write a python program to store first year percentage of students in array.

Write function

for sorting array of floating point numbers in ascending order using

a) Selection Sort

b) Bubble sort and display top five scores.

Program:
print("Selection sort")
marks=[]
n=int(input("Enter the number of students: "))
for i in range(n):
m=float(input("Enter the marks of students: "))
marks.append(m)
print("The array before sorting is: \n",marks)
def selection_sort(marks,size):
for i in range(size):
min_index=i
for j in range(i+1,size):
if marks[j]<marks[min_index]:
min_index=j
(marks[i],marks[min_index])=(marks[min_index],marks[i])

size=len(marks)
selection_sort(marks,size)
print("The array after sorting in ascending order is: \n",marks)
def max_marks(marks):
topper_list = []
topper_list=marks[-5:]
print("The five toppers are:\n",topper_list)
max_marks(marks)

print("**********************************************************
**")
print("Bubble Sort")
marks1=[]

n1=int(input("Enter the number of students: "))


for i in range(n1):
m1=float(input("Enter the marks of students: "))
marks1.append(m1)
print("The array before sorting is: \n",marks1)
def bubble_sort(marks1):
for i in range(0,len(marks1)-1):
for j in range(len(marks1)-1):
if(marks1[j]>marks1[j+1]):
temp=marks1[j]
marks1[j]=marks1[j+1]
marks1[j+1]=temp
return marks1

print("The sorted list is: \n",bubble_sort(marks1))


max_marks(marks1)

Output:
Selection sort
Enter the number of students: 7
Enter the marks of students: 22
Enter the marks of students: 88.66
Enter the marks of students: 77.44
Enter the marks of students: 66.22
Enter the marks of students: 44.11
Enter the marks of students: 11.76
Enter the marks of students: 33.89
The array before sorting is:
[22.0, 88.66, 77.44, 66.22, 44.11, 11.76, 33.89]
The array after sorting in ascending order is:
[11.76, 22.0, 33.89, 44.11, 66.22, 77.44, 88.66]
The five toppers are:
[33.89, 44.11, 66.22, 77.44, 88.66]
************************************************************
Bubble Sort
Enter the number of students: 7
Enter the marks of students: 99.99
Enter the marks of students: 44.55
Enter the marks of students: 22.44
Enter the marks of students: 33.77
Enter the marks of students: 68.39
Enter the marks of students: 55.20
Enter the marks of students: 11.45
The array before sorting is:
[99.99, 44.55, 22.44, 33.77, 68.39, 55.2, 11.45]
The sorted list is:
[11.45, 22.44, 33.77, 44.55, 55.2, 68.39, 99.99]
The five toppers are:
[33.77, 44.55, 55.2, 68.39, 99.99]

Process finished with exit code 0

You might also like