-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathbucket_sort.py
43 lines (27 loc) · 891 Bytes
/
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
import timeit
from random import randint
def bucket_sort(collection):
length = len(collection)
buckets_list = [0 for _ in range(length + 1)]
print("\tBuckets list before sorting - {}".format(buckets_list))
for j in range(length):
buckets_list[collection[j]] += 1
print("\tBuckets list after sorting - {}".format(buckets_list))
counter = 0
for i in range(length + 1):
for j in range(buckets_list[i]):
collection[counter] = i
counter += 1
return collection
def visualization():
length = 10
collection = [randint(0, length) for _ in range(length)]
print("Initial list:", collection)
print("Visualization of algorithm work.")
collection = bucket_sort(collection)
print("Final list:", collection)
def main():
elapsed_time = timeit.timeit(visualization, number=1)
print("Elapsed time: ", round(elapsed_time, 7), "sec.")
if __name__ == '__main__':
main()