-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBucket Sort.py
More file actions
67 lines (53 loc) · 1.8 KB
/
Copy pathBucket Sort.py
File metadata and controls
67 lines (53 loc) · 1.8 KB
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
def bucket_sort(Percentage):
largest = max(Percentage)
length = len(Percentage)
size = largest / length
# Create buckets
buckets = [[] for _ in range(length)]
# Distribute the elements into buckets
for i in range(length):
j = int(Percentage[i] / size)
if j != length:
buckets[j].append(Percentage[i])
else:
buckets[length - 1].append(Percentage[i])
# Sort each bucket using insertion sort
for i in range(length):
insertion_sort(buckets[i])
# Concatenate the sorted buckets into result
result = []
for i in range(length):
result = result + buckets[i]
return result
def insertion_sort(Percentage):
for i in range(1, len(Percentage)):
temp = Percentage[i]
j = i - 1
while j >= 0 and temp < Percentage[j]:
Percentage[j + 1] = Percentage[j]
j = j - 1
Percentage[j + 1] = temp
# Main block to input student percentages and use sorting
def main():
Percentage = []
number = int(input("Enter the Total Number of Students:\n"))
# Input percentages
for i in range(number):
value = float(input("Enter the Percentage:\n"))
Percentage.append(value)
# Sorting percentages using bucket sort
sorted_percentages = bucket_sort(Percentage)
# Display sorted percentages
print("Sorted Percentages:", sorted_percentages)
# Display top 5 scores
print("The Top five scores are:", sorted_percentages[-5:])
minimum = len(sorted_percentages) - 6
maximum = len(sorted_percentages) - 1
index = 1
for i in range(maximum, minimum, -1):
if i >= 0:
print(f"{index} Top Scorer: {sorted_percentages[i]}\n")
index += 1
# Run the main block
if __name__ == "__main__":
main()