-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathB14.py
executable file
·132 lines (98 loc) · 3.48 KB
/
B14.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# DSL – ASSIGNMENT 5 – B14
def bubbleSort(arr, n):
index = list(range(1, n + 1))
for i in range(n):
for j in range(0, n - i - 1):
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
index[j], index[j+1] = index[j+1], index[j]
return index
def selectionSort(arr, n):
index = list(range(1, n + 1))
for i in range(n):
min_idx = i
for j in range(i + 1, n):
if arr[min_idx] > arr[j]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
index[i], index[min_idx] = index[min_idx], index[i]
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)
arr2 = arr[:]
n = len(arr)
print('Sorting all the percentages using Bubble Sort:')
index = bubbleSort(arr, n)
putsorteddata(list(zip(index, arr)), n)
print('Sorting all the percentages using Selection Sort:')
index = selectionSort(arr2, n)
putsorteddata(list(zip(index, arr2)), n)
if __name__ == "__main__":
main()
"""
----------------- OUTPUT -----------------
Following are the percentages of all the students...
**********************************
| Roll No | Percentage |
**********************************
| 1 | 85% |
| 2 | 82% |
| 3 | 77% |
| 4 | 91% |
| 5 | 89% |
| 6 | 45% |
| 7 | 62% |
| 8 | 73% |
| 9 | 99% |
| 10 | 95% |
----------------------------------
Sorting all the percentages using Bubble Sort:
**********************************
| Roll No | Percentage |
**********************************
| 6 | 45% |
| 7 | 62% |
| 8 | 73% |
| 3 | 77% |
| 2 | 82% |
| 1 | 85% |
| 5 | 89% |
| 4 | 91% |
| 10 | 95% |
| 9 | 99% |
----------------------------------
Sorting all the percentages using Selection Sort:
**********************************
| Roll No | Percentage |
**********************************
| 6 | 45% |
| 7 | 62% |
| 8 | 73% |
| 3 | 77% |
| 2 | 82% |
| 1 | 85% |
| 5 | 89% |
| 4 | 91% |
| 10 | 95% |
| 9 | 99% |
----------------------------------
"""