-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathtask7.py
More file actions
49 lines (36 loc) · 1.18 KB
/
task7.py
File metadata and controls
49 lines (36 loc) · 1.18 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
N = int(input())
nums = [int(input()) for _ in range(N)]
def bucket_sort(nums, exp, buckets):
dlina = N
output = [0] * dlina
counter = [0] * 10
for i in range(dlina):
index_of_1_bucket = nums[i] // exp
counter[index_of_1_bucket % 10] += 1
for i in range(1, 10):
counter[i] += counter[i - 1]
for i in range(dlina - 1, -1, -1):
index_of_2_bucket = nums[i] // exp
output[counter[index_of_2_bucket % 10] - 1] = nums[i]
counter[index_of_2_bucket % 10] -= 1
for i in range(dlina):
nums[i] = output[i]
print("Bucket status:")
for i in range(10):
print(f"Bucket {i}: {', '.join(str(num) for num in buckets[i])}")
def secondary_sort(nums):
max_num = max(nums)
exp = 1
buckets = [[] for _ in range(10)]
print("Initial array:")
print(", ".join(str(num) for num in nums))
while max_num // exp > 0:
for num in nums:
buckets[num // exp % 10].append(num)
bucket_sort(nums, exp, buckets)
for i in range(10):
buckets[i] = []
exp *= 10
print("Sorted array:")
print(", ".join(str(num) for num in nums))
secondary_sort(nums)