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 Quick Sort And Display Top Five Scores

Given the  number of students in the class and their percentage as achieved in the first year of their term as an input to the Python program.  you should Sort the percentage  using a quick sort algorithm in ascending order and at the end you have to print the top five percentages.

As you are starting the percentage in ascending order in order to print the top five percent you have to print from the bottom.

 

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 Quick Sort And Display Top Five Scores

 

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 Quick Sort And Display Top Five Scores

Quicksort algorithm is a divide and conquer algorithm that divides the given array based on the pivot and then calls the quick sort algorithm recursively. Until the complete array is sorted.

In the below Quicksort algorithm the pivot is picked as the start element of the list.

Take a function named divide() and pass the  input argument as the list start and end index of the list.  take a variable name the pivot and assign the start element of the list take two indexes low and high.

def divide(my_list, start, end):
   pivot = my_list[start]
   low = start + 1
   high = end






Using the while loop iterate all the items from the end if you find the element which is less than pivot move the high pointer to that position

Using another  while loop iterate all the items from the start of the list and check if any item which is greater than the pivot move the low pointer to it

Swap the lower and higher position values

At the end of the while loop swap the pivot position with the higher position and return the high index

   while True:
       while low <= high and my_list[high] >= pivot:
           high = high - 1
       while low <= high and my_list[low] <= pivot:
           low = low + 1
       if low <= high:
           my_list[low], my_list[high] = my_list[high], my_list[low]
       else:
           break
   my_list[start], my_list[high] = my_list[high], my_list[start]
   return high





Use another function named as quick_sort() function,  pass the  list,  start index and end index as argument. Divide the list by calling the above function  with the help of pivot positions. Call the Quicksort function again  recursively for both the list.

def quick_sort(my_list, start, end):
   if start >= end:
       return

   d = divide(my_list, start, end)
   quick_sort(my_list, start, d - 1)
   quick_sort(my_list, d + 1, end)






In the main program take a variable length and after user to enter the number of students in the class.  take an empty list of percentages and use the for loop to iterate from 0 to list length and ask the user to enter the percentage of each student.

length = int(input("Enter Number of Students in Class : "))
percentages = []
for index in range(length):
   temp = float(input(f"Enter Student #{index} Percentage : "))
   percentages.append(temp)





Print a user friendly  message and call the quick sort function.  what is the  student percentage  are sorted using the for loop and list slicing technique get the last five percentages.

The last 5 percentages are the top 5 Percentage because you have sorted the array in ascending order(smallest to highest)

print("Top 5 Percentages of Students are ...")
quick_sort(percentages, 0, len(percentages) - 1)
for percentage in (percentages[::-1])[:5]:
   print(percentage)





Complete Python program
========================

def divide(my_list, start, end):
   pivot = my_list[start]
   low = start + 1
   high = end

   while True:
       while low <= high and my_list[high] >= pivot:
           high = high - 1
       while low <= high and my_list[low] <= pivot:
           low = low + 1
       if low <= high:
           my_list[low], my_list[high] = my_list[high], my_list[low]
       else:
           break
   my_list[start], my_list[high] = my_list[high], my_list[start]
   return high


def quick_sort(my_list, start, end):
   if start >= end:
       return

   d = divide(my_list, start, end)
   quick_sort(my_list, start, d - 1)
   quick_sort(my_list, d + 1, end)


length = int(input("Enter Number of Students in Class : "))
percentages = []
for index in range(length):
   temp = float(input(f"Enter Student #{index} Percentage : "))
   percentages.append(temp)

print("Top 5 Percentages of Students are ...")
quick_sort(percentages, 0, len(percentages) - 1)
for percentage in (percentages[::-1])[:5]:
   print(percentage)






OUTPUT
=================

Enter Number of Students in Class : 10
Enter Student #0 Percentage : 99
Enter Student #1 Percentage : 90
Enter Student #2 Percentage : 80
Enter Student #3 Percentage : 76
Enter Student #4 Percentage : 75
Enter Student #5 Percentage : 74
Enter Student #6 Percentage : 73
Enter Student #7 Percentage : 72
Enter Student #8 Percentage : 71
Enter Student #9 Percentage : 50
Top 5 Percentages of Students are ...
99.0
90.0
80.0
76.0
75.0




Conclusion
====================

Comment down below if you have any suggestions to improve the above Python program.
 
 
 


Post a Comment

0 Comments