Fixed quick sort and added top 5
This commit is contained in:
parent
c14ce1a405
commit
9b44a5dfc6
@ -1,39 +1,31 @@
|
||||
no = int(input("Enter number of students: "))
|
||||
list2 = []
|
||||
# Function for quick sort:
|
||||
def quicksort(arr):
|
||||
if len(arr) <= 1:
|
||||
return arr
|
||||
|
||||
for i in range(0,no):
|
||||
n = float(input("Enter % of student: "))
|
||||
list2.append(n)
|
||||
print(list2)
|
||||
pivot = arr[len(arr) // 2]
|
||||
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]
|
||||
|
||||
d = len(list2)
|
||||
return quicksort(left) + middle + quicksort(right)
|
||||
|
||||
def partition(list3, low, high):
|
||||
i = low - 1
|
||||
pivot = list3[high]
|
||||
# Function for displaying top scores:
|
||||
def top5(arr):
|
||||
sorted_arr = quicksort(arr)
|
||||
top_scores = sorted_arr[-5:][::-1]
|
||||
return top_scores
|
||||
|
||||
for j in range(low, high):
|
||||
if list3[j] < pivot:
|
||||
i += 1
|
||||
list3[i], list3[j] = list3[j], list3[i]
|
||||
|
||||
list3[i + 1], list3[high] = list3[high], list3[i + 1]
|
||||
return i + 1
|
||||
|
||||
def quicksort(list1, low, high):
|
||||
if low < high:
|
||||
pi = partition(list1, low, high)
|
||||
quicksort(list1, low, pi - 1)
|
||||
quicksort(list1, pi + 1, high)
|
||||
|
||||
no = int(input("Enter number of students: "))
|
||||
list2 = []
|
||||
|
||||
for i in range(0, no):
|
||||
n = float(input("Enter % of student: "))
|
||||
list2.append(n)
|
||||
print("Unsorted list: ", list2)
|
||||
|
||||
quicksort(list2, 0, len(list2) - 1)
|
||||
print("Sorted list: ", list2)
|
||||
# Defining main function:
|
||||
def main():
|
||||
total=int(input("Total number of students are:\t"))
|
||||
percent=[]
|
||||
for i in range(total):
|
||||
percent_in=float(input(f"Enter percentage for student {i+1}:\t"))
|
||||
percent.append(percent_in)
|
||||
print(f"\nPercentages of students are:\t {percent}")
|
||||
print(f"\n------------------\nSorted marks (using quick sort algorithm):\t{quicksort(percent)}\n------------------")
|
||||
print(f"\n------------------\nTop five scores are:\t{top5(percent)}\n------------------\n")
|
||||
|
||||
# Calling main function:
|
||||
main()
|
||||
|
Loading…
Reference in New Issue
Block a user