SE Computer Engineering Practical 11 solution

Home » All Blogs » Python » Python assignments » SE Computer Engineering » SE Computer Engineering Practical 11 solution

SE Computer Engineering Practical 11 solution

Question:

Write a python program to store first year percentage of students in an 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

Code:

def Selection_Sort(marks):
   for i in range(len(marks)):

       # Find the minimum element in remaining unsorted array
       min_idx = i
       for j in range(i + 1, len(marks)):
           if marks[min_idx] > marks[j]:
               min_idx = j

       # Swap the minimum element with the first element
       marks[i], marks[min_idx] = marks[min_idx], marks[i]

   print("Marks of students after performing Selection Sort on the list : ")
   for i in range(len(marks)):
       print(marks[i])


# Function for Bubble Sort of elements

def Bubble_Sort(marks):
   n = len(marks)
   # Traverse through all array elements
   for i in range(n - 1):
       # Last i elements are already in place
       for j in range(0, n - i - 1):

           # Traverse the array from 0 to n-i-1
           # Swap if the element found is greater than the next element
           if marks[j] > marks[j + 1]:
               marks[j], marks[j + 1] = marks[j + 1], marks[j]

   print("Marks of students after performing Bubble Sort on the list :")
   for i in range(len(marks)):
       print(marks[i])


# Function for displaying top five marks

def top_five_marks(marks):
   print("Top", len(marks), "Marks are : ")
   print(*marks[::-1], sep="\n")


marks = []
n = int(input("Enter number of students whose marks are to be displayed : "))
print("Enter marks for", n, "students (Press ENTER after every students marks): ")
for i in range(0, n):
   ele = int(input())
   marks.append(ele)  # adding the element

print("The marks of", n, "students are : ")
print(marks)

flag = 1
while flag == 1:
   print("\n---------------MENU---------------")
   print("1. Selection Sort of the marks")
   print("2. Bubble Sort of the marks")
   print("3. Exit")
   ch = int(input("\n\nEnter your choice (from 1 to 3) : "))

   if ch == 1:
       Selection_Sort(marks)
       a = input("\nDo you want to display top marks from the list (yes/no) : ")
       if a == 'yes':
           top_five_marks(marks)
       else:
           print("\nThanks for using this program!")
           flag = 0

   elif ch == 2:
       Bubble_Sort(marks)
       a = input("\nDo you want to display top five marks from the list (yes/no) : ")
       if a == 'yes':
           top_five_marks(marks)
       else:
           print("\nThanks for using this program!")
           flag = 0

   elif ch == 3:
       print("\nThanks for using this program!!")
       flag = 0

   else:
       print("\nEnter a valid choice!!")
       print("\nThanks for using this program!!")
       flag = 0

Output:

Enter number of students whose marks are to be displayed : 5
Enter marks for 5 students (Press ENTER after every students marks): 
50
43
76
88
67
The marks of 5 students are : 
[50, 43, 76, 88, 67]

---------------MENU---------------
1. Selection Sort of the marks
2. Bubble Sort of the marks
3. Exit


Enter your choice (from 1 to 3) : 1
Marks of students after performing Selection Sort on the list : 
43
50
67
76
88

Do you want to display top marks from the list (yes/no) : yes
Top 5 Marks are : 
88
76
67
50
43

---------------MENU---------------
1. Selection Sort of the marks
2. Bubble Sort of the marks
3. Exit


Enter your choice (from 1 to 3) : 2
Marks of students after performing Bubble Sort on the list :
43
50
67
76
88

Do you want to display top five marks from the list (yes/no) : no

Thanks for using this program!

Process finished with exit code 0
Tech Amplifier Final Logo