2023-12-10 19:32:22 +05:30
|
|
|
# BEGINNING OF CODE
|
2023-10-16 19:32:29 +05:30
|
|
|
# Function for quick sort:
|
2023-12-10 19:32:22 +05:30
|
|
|
def quickSort(arr):
|
|
|
|
if len(arr) <= 1: # if array is empty array
|
2023-10-16 19:32:29 +05:30
|
|
|
return arr
|
|
|
|
|
2023-12-10 19:32:22 +05:30
|
|
|
pivot = arr[len(arr) // 2] # Set pivot element
|
|
|
|
|
2023-10-16 19:32:29 +05:30
|
|
|
left = [x for x in arr if x < pivot]
|
|
|
|
middle = [x for x in arr if x == pivot]
|
|
|
|
right = [x for x in arr if x > pivot]
|
2023-12-10 19:32:22 +05:30
|
|
|
'''
|
|
|
|
## ALTERNATIVE WAY OF WRITING THE UPPER 3 LINES (for easy understanding)
|
|
|
|
left = [] # Empty list to store left part
|
2023-12-11 09:47:09 +05:30
|
|
|
middle = [] # Empty list to store middle element
|
|
|
|
right = [] # Empty list to store right part
|
2023-12-10 19:32:22 +05:30
|
|
|
for i in arr:
|
|
|
|
if (i < pivot):
|
|
|
|
left.append(i)
|
2023-12-11 09:47:09 +05:30
|
|
|
elif (i == pivot):
|
2023-12-10 19:32:22 +05:30
|
|
|
middle.append(i)
|
2023-12-11 09:47:09 +05:30
|
|
|
else:
|
2023-12-10 19:32:22 +05:30
|
|
|
right.append(i)
|
|
|
|
'''
|
2023-10-16 19:32:29 +05:30
|
|
|
|
2023-12-10 19:32:22 +05:30
|
|
|
return quickSort(left) + middle + quickSort(right)
|
2023-10-16 19:32:29 +05:30
|
|
|
|
|
|
|
# Function for displaying top scores:
|
|
|
|
def top5(arr):
|
2023-12-10 19:32:22 +05:30
|
|
|
top_scores = quickSort(arr)
|
|
|
|
return top_scores[-1:-6:-1]
|
2023-10-16 19:32:29 +05:30
|
|
|
|
|
|
|
# Defining main function:
|
|
|
|
def main():
|
2023-12-10 19:32:22 +05:30
|
|
|
percent = [] # List to store percentages
|
|
|
|
|
|
|
|
total = int(input("Total number of students are:\t")) # Input for total students
|
2023-10-16 19:32:29 +05:30
|
|
|
for i in range(total):
|
2023-12-10 19:32:22 +05:30
|
|
|
percent_in = float(input(f"Enter percentage for student {i+1}:\t"))
|
2023-10-16 19:32:29 +05:30
|
|
|
percent.append(percent_in)
|
2023-12-10 19:32:22 +05:30
|
|
|
|
|
|
|
print(f"-----\nPercentages of students are:\t {percent}\n-----")
|
|
|
|
print(f"\n-----\nSorted marks (using quick sort algorithm):\t{quickSort(percent)}\n-----")
|
|
|
|
print(f"\n-----\nTop five scores are:\t{top5(percent)}\n-----")
|
2023-10-16 19:32:29 +05:30
|
|
|
|
|
|
|
# Calling main function:
|
|
|
|
main()
|
2023-12-11 09:47:09 +05:30
|
|
|
# END OF CODE
|