Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sorts/bucket_sort.py implementation #5786

Merged
merged 5 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@
* [2 Hidden Layers Neural Network](neural_network/2_hidden_layers_neural_network.py)
* Activation Functions
* [Exponential Linear Unit](neural_network/activation_functions/exponential_linear_unit.py)
* [Leaky Rectified Linear Unit](neural_network/activation_functions/leaky_rectified_linear_unit.py)
* [Back Propagation Neural Network](neural_network/back_propagation_neural_network.py)
* [Convolution Neural Network](neural_network/convolution_neural_network.py)
* [Perceptron](neural_network/perceptron.py)
Expand Down
18 changes: 12 additions & 6 deletions sorts/bucket_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from __future__ import annotations


def bucket_sort(my_list: list) -> list:
def bucket_sort(my_list: list, bucket_count: int = 10) -> list:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why bucket count is set to 10?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some default value. It can be 100 or whatever.
Or we can set it to None by default and calculate it by some heuristic if it's not set, ex. len(my_list) // 2.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that rather this having bucket_count as a parameter, we can have it as part of implementation logic where we calculate bucket count based on type of machine 32 for 32bit, 64 for 64bit. In that way we eliminate user driven errors

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nit-picky / low-priority -
Can we change variable name to something more descriptive? Maybe have list_to_sort instead of my_list?

"""
>>> data = [-1, 2, -5, 0]
>>> bucket_sort(data) == sorted(data)
Expand All @@ -43,21 +43,27 @@ def bucket_sort(my_list: list) -> list:
True
>>> bucket_sort([]) == sorted([])
True
>>> data = [-1e10, 1e10]
>>> bucket_sort(data) == sorted(data)
True
>>> import random
>>> collection = random.sample(range(-50, 50), 50)
>>> bucket_sort(collection) == sorted(collection)
True
"""
if len(my_list) == 0:

if len(my_list) == 0 or bucket_count <= 0:
return []

min_value, max_value = min(my_list), max(my_list)
bucket_count = int(max_value - min_value) + 1
bucket_size = (max_value - min_value) / bucket_count
buckets: list[list] = [[] for _ in range(bucket_count)]

for i in my_list:
buckets[int(i - min_value)].append(i)
for val in my_list:
index = min(int((val - min_value) / bucket_size), bucket_count - 1)
buckets[index].append(val)

return [v for bucket in buckets for v in sorted(bucket)]
return [val for bucket in buckets for val in sorted(bucket)]


if __name__ == "__main__":
Expand Down