Professional Documents
Culture Documents
Assi 14
Assi 14
Write function
a) Selection Sort
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=[]
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]