DSL/assignment-14.py

62 lines
1.7 KiB
Python
Raw Normal View History

2023-09-25 17:54:20 +05:30
# Sorting (Selection+Bubble) and Top 5
marks=[]
# Function to enter marks of students
def input_marks():
students=int(input("Enter the number of students:\t"))
for i in range(students):
marks_in=float(input("Enter the marks (out of 50):\t"))
marks.append(marks_in)
print("\nThe marks you've entered for ", students, "students are: ", marks, "\n")
# Function for selection sort
def selection():
for i in range(len(marks)):
min_index=i
for j in range(i+1, len(marks)):
if marks[j] < marks[min_index]:
min_index=j
marks[i], marks[min_index] = marks[min_index], marks[i]
print("Marks sorted in ascending order using selection sort:\t", marks)
def bubble():
for i in range(len(marks)):
for j in range(0, len(marks)-i-1):
if marks[j]>marks[j+1]:
marks[j], marks[j+1] = marks[j+1], marks[j]
print("Marks sorted in ascending order using bubble sort:\t", marks)
def top5():
for i in range(len(marks)):
for j in range(0, len(marks)-i-1):
if marks[j]<marks[j+1]:
2023-09-25 17:54:20 +05:30
marks[j], marks[j+1] = marks[j+1], marks[j]
print("Top 5 marks using bubble sorting:\t", marks[0:5])
2023-09-25 17:54:20 +05:30
def choose_optn():
while True:
print("Choose an option from the menu below:")
print("1 -> Input marks")
print("2 -> Selection Sorting")
print("3 -> Bubble Sorting")
print("4 -> Display top 5")
print("5 -> Exit")
optn=int(input("Choose an option (1-5):\t"))
if optn==1:
input_marks()
elif optn==2:
selection()
elif optn==3:
bubble()
elif optn==4:
top5()
elif optn==5:
print("\n## END OF CODE\n\n")
quit()
2023-09-25 17:54:20 +05:30
else:
print("\nPlease choose a valid option (1-5).\n")
choose_optn()