-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
tianyizheng02
merged 5 commits into
TheAlgorithms:master
from
drinkertea:fix_bucket_sort
Aug 18, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
92e7918
Fix sorts/bucket_sort.py
drinkertea 4a376c6
Merge branch 'master' into fix_bucket_sort
tianyizheng02 c2f569b
updating DIRECTORY.md
ce9a8e8
Remove unused var in bucket_sort.py
tianyizheng02 5ab1ccc
Fix list index in bucket_sort.py
tianyizheng02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,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__": | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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