2020-06-17 07:42:44 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
Illustrate how to implement bucket sort algorithm.
|
2017-07-20 01:29:42 +00:00
|
|
|
|
2020-06-17 07:42:44 +00:00
|
|
|
Author: OMKAR PATHAK
|
|
|
|
This program will illustrate how to implement bucket sort algorithm
|
|
|
|
|
|
|
|
Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works
|
|
|
|
by distributing the elements of an array into a number of buckets.
|
|
|
|
Each bucket is then sorted individually, either using a different sorting
|
|
|
|
algorithm, or by recursively applying the bucket sorting algorithm. It is a
|
|
|
|
distribution sort, and is a cousin of radix sort in the most to least
|
|
|
|
significant digit flavour.
|
|
|
|
Bucket sort is a generalization of pigeonhole sort. Bucket sort can be
|
|
|
|
implemented with comparisons and therefore can also be considered a
|
|
|
|
comparison sort algorithm. The computational complexity estimates involve the
|
|
|
|
number of buckets.
|
|
|
|
|
|
|
|
Time Complexity of Solution:
|
|
|
|
Worst case scenario occurs when all the elements are placed in a single bucket.
|
|
|
|
The overall performance would then be dominated by the algorithm used to sort each
|
|
|
|
bucket. In this case, O(n log n), because of TimSort
|
|
|
|
|
|
|
|
Average Case O(n + (n^2)/k + k), where k is the number of buckets
|
|
|
|
|
|
|
|
If k = O(n), time complexity is O(n)
|
|
|
|
|
|
|
|
Source: https://en.wikipedia.org/wiki/Bucket_sort
|
|
|
|
"""
|
2019-07-01 08:10:18 +00:00
|
|
|
DEFAULT_BUCKET_SIZE = 5
|
|
|
|
|
2019-05-19 09:00:54 +00:00
|
|
|
|
2020-06-17 07:42:44 +00:00
|
|
|
def bucket_sort(my_list: list, bucket_size: int = DEFAULT_BUCKET_SIZE) -> list:
|
|
|
|
"""
|
|
|
|
>>> data = [-1, 2, -5, 0]
|
|
|
|
>>> bucket_sort(data) == sorted(data)
|
|
|
|
True
|
|
|
|
|
|
|
|
>>> data = [9, 8, 7, 6, -12]
|
|
|
|
>>> bucket_sort(data) == sorted(data)
|
|
|
|
True
|
|
|
|
|
|
|
|
>>> data = [.4, 1.2, .1, .2, -.9]
|
|
|
|
>>> bucket_sort(data) == sorted(data)
|
|
|
|
True
|
|
|
|
|
|
|
|
>>> bucket_sort([])
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
Exception: Please add some elements in the array.
|
|
|
|
"""
|
2019-05-22 12:09:36 +00:00
|
|
|
if len(my_list) == 0:
|
|
|
|
raise Exception("Please add some elements in the array.")
|
2019-05-19 09:00:54 +00:00
|
|
|
|
2019-05-22 12:09:36 +00:00
|
|
|
min_value, max_value = (min(my_list), max(my_list))
|
2019-10-05 05:14:13 +00:00
|
|
|
bucket_count = (max_value - min_value) // bucket_size + 1
|
2019-05-22 12:09:36 +00:00
|
|
|
buckets = [[] for _ in range(int(bucket_count))]
|
2019-05-19 09:00:54 +00:00
|
|
|
|
|
|
|
for i in range(len(my_list)):
|
2019-10-05 05:14:13 +00:00
|
|
|
buckets[int((my_list[i] - min_value) // bucket_size)].append(my_list[i])
|
2019-05-19 09:00:54 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
return sorted(
|
2020-06-17 07:42:44 +00:00
|
|
|
buckets[i][j] for i in range(len(buckets)) for j in range(len(buckets[i]))
|
2019-10-05 05:14:13 +00:00
|
|
|
)
|
2019-07-01 08:10:18 +00:00
|
|
|
|
2019-05-19 09:00:54 +00:00
|
|
|
|
2019-05-22 12:09:36 +00:00
|
|
|
if __name__ == "__main__":
|
2020-06-17 07:42:44 +00:00
|
|
|
assert bucket_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5]
|
|
|
|
assert bucket_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15]
|