-
Notifications
You must be signed in to change notification settings - Fork 45
/
B16.py
executable file
·108 lines (79 loc) · 2.81 KB
/
B16.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# DSL – ASSIGNMENT 6 – B16
def partition(arr, index, low, high):
i = (low - 1)
pivot = arr[high]
for j in range(low , high):
if arr[j] < pivot:
i = i + 1
arr[i], arr[j] = arr[j], arr[i]
index[i], index[j] = index[j], index[i]
arr[i + 1], arr[high] = arr[high], arr[i + 1]
index[i + 1], index[high] = index[high], index[i + 1]
return i + 1
def quickSort(arr, index, low, high):
if low < high:
pi = partition(arr, index, low, high)
quickSort(arr, index, low, pi - 1)
quickSort(arr, index, pi + 1, high)
return index
def putdata(arr, n):
print('\n\nFollowing are the percentages of all the students...\n')
print('**********************************')
print('| Roll No | Percentage |')
print('**********************************')
for i in range(n):
print(f'|\t{i + 1}\t|\t{arr[i]}%\t |')
print('----------------------------------\n\n')
def putsorteddata(arr, n):
print('\n**********************************')
print('| Roll No | Percentage |')
print('**********************************')
for i in range(n):
print(f'|\t{arr[i][0]}\t|\t{arr[i][1]}%\t |')
print('----------------------------------\n\n')
def main():
n = int(input('\nEnter number of student in 1st year: '))
arr = []
for i in range(n):
arr.append(int(input(f'Enter percentage of roll no {i + 1}: ')))
putdata(arr, n)
n = len(arr)
index = list(range(1, n + 1))
print('Sorting all the percentages using Quick Sort:')
index = quickSort(arr, index, 0, n - 1)
putsorteddata(list(zip(index, arr)), n)
if __name__ == "__main__":
main()
"""
----------------- OUTPUT -----------------
Following are the percentages of all the students...
**********************************
| Roll No | Percentage |
**********************************
| 1 | 85.6% |
| 2 | 82.8% |
| 3 | 77.7% |
| 4 | 91.9% |
| 5 | 89.08% |
| 6 | 45.88% |
| 7 | 62.4% |
| 8 | 73.5% |
| 9 | 99.02% |
| 10 | 95.6% |
----------------------------------
Sorting all the percentages using Quick Sort:
**********************************
| Roll No | Percentage |
**********************************
| 6 | 45.88% |
| 7 | 62.4% |
| 8 | 73.5% |
| 3 | 77.7% |
| 2 | 82.8% |
| 1 | 85.6% |
| 5 | 89.08% |
| 4 | 91.9% |
| 10 | 95.6% |
| 9 | 99.02% |
----------------------------------
"""