Skip to content

Commit 72c7b05

Browse files
drinkerteatianyizheng02github-actions
authored
Fix sorts/bucket_sort.py implementation (#5786)
* Fix sorts/bucket_sort.py * updating DIRECTORY.md * Remove unused var in bucket_sort.py * Fix list index in bucket_sort.py --------- Co-authored-by: Tianyi Zheng <[email protected]> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent a207187 commit 72c7b05

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

Diff for: DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@
710710
* [2 Hidden Layers Neural Network](neural_network/2_hidden_layers_neural_network.py)
711711
* Activation Functions
712712
* [Exponential Linear Unit](neural_network/activation_functions/exponential_linear_unit.py)
713+
* [Leaky Rectified Linear Unit](neural_network/activation_functions/leaky_rectified_linear_unit.py)
713714
* [Back Propagation Neural Network](neural_network/back_propagation_neural_network.py)
714715
* [Convolution Neural Network](neural_network/convolution_neural_network.py)
715716
* [Perceptron](neural_network/perceptron.py)

Diff for: sorts/bucket_sort.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from __future__ import annotations
3131

3232

33-
def bucket_sort(my_list: list) -> list:
33+
def bucket_sort(my_list: list, bucket_count: int = 10) -> list:
3434
"""
3535
>>> data = [-1, 2, -5, 0]
3636
>>> bucket_sort(data) == sorted(data)
@@ -43,21 +43,27 @@ def bucket_sort(my_list: list) -> list:
4343
True
4444
>>> bucket_sort([]) == sorted([])
4545
True
46+
>>> data = [-1e10, 1e10]
47+
>>> bucket_sort(data) == sorted(data)
48+
True
4649
>>> import random
4750
>>> collection = random.sample(range(-50, 50), 50)
4851
>>> bucket_sort(collection) == sorted(collection)
4952
True
5053
"""
51-
if len(my_list) == 0:
54+
55+
if len(my_list) == 0 or bucket_count <= 0:
5256
return []
57+
5358
min_value, max_value = min(my_list), max(my_list)
54-
bucket_count = int(max_value - min_value) + 1
59+
bucket_size = (max_value - min_value) / bucket_count
5560
buckets: list[list] = [[] for _ in range(bucket_count)]
5661

57-
for i in my_list:
58-
buckets[int(i - min_value)].append(i)
62+
for val in my_list:
63+
index = min(int((val - min_value) / bucket_size), bucket_count - 1)
64+
buckets[index].append(val)
5965

60-
return [v for bucket in buckets for v in sorted(bucket)]
66+
return [val for bucket in buckets for val in sorted(bucket)]
6167

6268

6369
if __name__ == "__main__":

0 commit comments

Comments
 (0)