-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Changes from 1 commit
92e7918
4a376c6
c2f569b
ce9a8e8
5ab1ccc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. very nit-picky / low-priority - |
||
""" | ||
>>> data = [-1, 2, -5, 0] | ||
>>> bucket_sort(data) == sorted(data) | ||
|
@@ -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)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.There was a problem hiding this comment.
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