Skip to content

Commit d7a75da

Browse files
l3str4ngecclaussgithub-actions
authored
Added doctests to bucket sort (TheAlgorithms#2079)
* Added doctests to bucket sort * Missing typehint * Wrap long lines * updating DIRECTORY.md * Update bucket_sort.py * updating DIRECTORY.md * Update bucket_sort.py Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 6f80ca8 commit d7a75da

File tree

2 files changed

+52
-31
lines changed

2 files changed

+52
-31
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
* [Trie](https://github.com/TheAlgorithms/Python/blob/master/data_structures/trie/trie.py)
148148

149149
## Digital Image Processing
150+
* [Change Brightness](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/change_brightness.py)
150151
* [Change Contrast](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/change_contrast.py)
151152
* [Convert To Negative](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/convert_to_negative.py)
152153
* Dithering
@@ -268,6 +269,7 @@
268269
## Hashes
269270
* [Adler32](https://github.com/TheAlgorithms/Python/blob/master/hashes/adler32.py)
270271
* [Chaos Machine](https://github.com/TheAlgorithms/Python/blob/master/hashes/chaos_machine.py)
272+
* [Djb2](https://github.com/TheAlgorithms/Python/blob/master/hashes/djb2.py)
271273
* [Enigma Machine](https://github.com/TheAlgorithms/Python/blob/master/hashes/enigma_machine.py)
272274
* [Hamming Code](https://github.com/TheAlgorithms/Python/blob/master/hashes/hamming_code.py)
273275
* [Md5](https://github.com/TheAlgorithms/Python/blob/master/hashes/md5.py)

sorts/bucket_sort.py

+50-31
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,54 @@
1-
#!/usr/bin/env python
2-
3-
"""Illustrate how to implement bucket sort algorithm."""
4-
5-
# Author: OMKAR PATHAK
6-
# This program will illustrate how to implement bucket sort algorithm
7-
8-
# Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works
9-
# by distributing the elements of an array into a number of buckets.
10-
# Each bucket is then sorted individually, either using a different sorting
11-
# algorithm, or by recursively applying the bucket sorting algorithm. It is a
12-
# distribution sort, and is a cousin of radix sort in the most to least
13-
# significant digit flavour.
14-
# Bucket sort is a generalization of pigeonhole sort. Bucket sort can be
15-
# implemented with comparisons and therefore can also be considered a
16-
# comparison sort algorithm. The computational complexity estimates involve the
17-
# number of buckets.
18-
19-
# Time Complexity of Solution:
20-
# Worst case scenario occurs when all the elements are placed in a single bucket. The
21-
# overall performance would then be dominated by the algorithm used to sort each bucket.
22-
# In this case, O(n log n), because of TimSort
23-
#
24-
# Average Case O(n + (n^2)/k + k), where k is the number of buckets
25-
#
26-
# If k = O(n), time complexity is O(n)
1+
#!/usr/bin/env python3
2+
"""
3+
Illustrate how to implement bucket sort algorithm.
274
5+
Author: OMKAR PATHAK
6+
This program will illustrate how to implement bucket sort algorithm
7+
8+
Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works
9+
by distributing the elements of an array into a number of buckets.
10+
Each bucket is then sorted individually, either using a different sorting
11+
algorithm, or by recursively applying the bucket sorting algorithm. It is a
12+
distribution sort, and is a cousin of radix sort in the most to least
13+
significant digit flavour.
14+
Bucket sort is a generalization of pigeonhole sort. Bucket sort can be
15+
implemented with comparisons and therefore can also be considered a
16+
comparison sort algorithm. The computational complexity estimates involve the
17+
number of buckets.
18+
19+
Time Complexity of Solution:
20+
Worst case scenario occurs when all the elements are placed in a single bucket.
21+
The overall performance would then be dominated by the algorithm used to sort each
22+
bucket. In this case, O(n log n), because of TimSort
23+
24+
Average Case O(n + (n^2)/k + k), where k is the number of buckets
25+
26+
If k = O(n), time complexity is O(n)
27+
28+
Source: https://en.wikipedia.org/wiki/Bucket_sort
29+
"""
2830
DEFAULT_BUCKET_SIZE = 5
2931

3032

31-
def bucket_sort(my_list, bucket_size=DEFAULT_BUCKET_SIZE):
33+
def bucket_sort(my_list: list, bucket_size: int = DEFAULT_BUCKET_SIZE) -> list:
34+
"""
35+
>>> data = [-1, 2, -5, 0]
36+
>>> bucket_sort(data) == sorted(data)
37+
True
38+
39+
>>> data = [9, 8, 7, 6, -12]
40+
>>> bucket_sort(data) == sorted(data)
41+
True
42+
43+
>>> data = [.4, 1.2, .1, .2, -.9]
44+
>>> bucket_sort(data) == sorted(data)
45+
True
46+
47+
>>> bucket_sort([])
48+
Traceback (most recent call last):
49+
...
50+
Exception: Please add some elements in the array.
51+
"""
3252
if len(my_list) == 0:
3353
raise Exception("Please add some elements in the array.")
3454

@@ -40,11 +60,10 @@ def bucket_sort(my_list, bucket_size=DEFAULT_BUCKET_SIZE):
4060
buckets[int((my_list[i] - min_value) // bucket_size)].append(my_list[i])
4161

4262
return sorted(
43-
[buckets[i][j] for i in range(len(buckets)) for j in range(len(buckets[i]))]
63+
buckets[i][j] for i in range(len(buckets)) for j in range(len(buckets[i]))
4464
)
4565

4666

4767
if __name__ == "__main__":
48-
user_input = input("Enter numbers separated by a comma:").strip()
49-
unsorted = [float(n) for n in user_input.split(",") if len(user_input) > 0]
50-
print(bucket_sort(unsorted))
68+
assert bucket_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5]
69+
assert bucket_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15]

0 commit comments

Comments
 (0)