-
Notifications
You must be signed in to change notification settings - Fork 0
/
LA8.py
94 lines (60 loc) · 2.22 KB
/
LA8.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def input_percentage():
perc = []
number_of_students = int(input("\nEnter the number of Students : "))
for i in range(number_of_students):
perc.append(float(input("Enter the percentage of Student {0} : ".format(i+1))))
return perc
def print_percentage(perc):
for i in range(len(perc)):
print(perc[i],sep = "\n")
def percentage_partition(perc,start,end):
pivot = perc[start]
lower_bound = start + 1
upper_bound = end
while True:
while lower_bound <= upper_bound and perc[lower_bound] <= pivot:
lower_bound += 1
while lower_bound <= upper_bound and perc[upper_bound] >= pivot:
upper_bound -= 1
if lower_bound <= upper_bound:
perc[lower_bound],perc[upper_bound] = perc[upper_bound],perc[lower_bound]
else:
break
perc[start],perc[upper_bound] = perc[upper_bound],perc[start]
return upper_bound
def Quick_Sort(perc,start,end):
while start < end:
partition = percentage_partition(perc,start,end)
Quick_Sort(perc,start,partition-1)
Quick_Sort(perc,partition+1,end)
return perc
def topfive(perc):
print("\nTop Five Students : ")
if len(perc) < 5:
start, stop = len(perc) - 1, -1
else:
start, stop = len(perc) - 1, len(perc) - 6
for i in range(start, stop, -1):
print(perc[i],sep = "\n")
unsorted_percentage = []
sorted_percentage = []
flag = 1
while flag == 1:
print("\n1. Accept the Percentage of Students")
print("2. Display the Percentages of Students")
print("3. Perform Quick Sort ")
print("4. Display top 5 students")
print("5. Exit")
ch = int(input("\nEnter the choice : "))
if ch == 1:
unsorted_percentage = input_percentage()
elif ch == 2:
print_percentage(unsorted_percentage)
elif ch == 3:
print("\nPercentages after sorting : ")
sorted_percentage = Quick_Sort(unsorted_percentage,0,len(unsorted_percentage)-1)
print_percentage(sorted_percentage)
elif ch == 4:
topfive(sorted_percentage)
else:
break