Skip to content

Fix sorts/bucket_sort.py implementation #5786

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

Merged
merged 5 commits into from
Aug 18, 2023
Merged
Changes from 1 commit
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
14 changes: 10 additions & 4 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,19 +43,25 @@ 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 range(len(my_list)):
buckets[(int(my_list[i] - min_value) // bucket_count)].append(my_list[i])
index = min(int((my_list[i] - min_value) / bucket_size), bucket_count - 1)
buckets[index].append(my_list[i])

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

Choose a reason for hiding this comment

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

Can we recursively call bucket sort rather than relying on sorted method? In that way we can eliminate dependence on inbuilt python methods

''' pseudo-code
return [v for bucket in buckets for v in bucket_sort(bucket)]
'''


Expand Down