-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbucket_sort.py
71 lines (55 loc) · 1.65 KB
/
bucket_sort.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
"""A simple implementation of the bucket sort algorithm.
Bucket sort is a distribution algorithm that involves three steps:
1. Scatter - distribute keys to buckets
2. Sort - sort keys within each bucket
3. Gather - gather the sorted keys in order
This code is based on the description of the algorithm in the following sources:
- https://en.wikipedia.org/wiki/bucket_sort
- Chapter 8 of T. H. Cormen, et al., "Introduction to algorithms", MIT press (2022)
"""
def bucket_sort(A: list):
"""Sort the given input A using bucket sort.
Args:
A: the array to be sorted.
Returns:
The sorted array.
"""
num_buckets = len(A)
buckets = [[] for _ in range(num_buckets)]
for key in A: # scatter
buckets[int(num_buckets * key)].append(key)
for bucket in buckets:
insertion_sort(bucket)
return [x for bucket in buckets for x in bucket] # gather
def insertion_sort(A: list):
"""Sort the given input A using insertion sort.
Args:
A: the array to be sorted.
Returns:
The sorted array.
"""
for j in range(1, len(A)):
key = A[j]
i = j - 1
while i >= 0 and A[i] > key:
A[i + 1] = A[i]
i -= 1
A[i + 1] = key
return A
def main():
A = [0.15, 0.4, 0.18, 0.8, 0.13]
# the number of digits in each key
print("Array to be sorted:")
print(A)
A = bucket_sort(A)
print("Array sorted with bucket sort:")
print(A)
"""
Print out >>>
Array to be sorted:
[0.15, 0.4, 0.18, 0.8, 0.13]
Array sorted with bucket sort:
[0.13, 0.15, 0.18, 0.4, 0.8]
"""
if __name__ == "__main__":
main()