Skip to content

Added bead sort #2022

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 4 commits into from
May 22, 2020
Merged

Added bead sort #2022

merged 4 commits into from
May 22, 2020

Conversation

l3str4nge
Copy link
Member

Describe your change:

Added bead sort aka gravity sort

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.


def bead_sort(sequence: list) -> list:
"""
>>> bead_sort([6,11, 12, 4, 1, 5])
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
>>> bead_sort([6,11, 12, 4, 1, 5])
>>> bead_sort([6, 11, 12, 4, 1, 5])

Copy link
Member

Choose a reason for hiding this comment

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

Please test [1, .9, 0.0, 0, -1, -.9] and "Hello World!"

Copy link
Member Author

@l3str4nge l3str4nge May 21, 2020

Choose a reason for hiding this comment

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

I am wondering on "Hello world" ordering. Theory says that algorithm should be use for positive integers. I think checking types inside sort function is not efficient.

Copy link
Member Author

Choose a reason for hiding this comment

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

After a few moments, I decide to write two checks for it. I don't want to argue with the theory! 😄 @cclauss maybe you have a better idea for this?

Comment on lines 28 to 32
if isinstance(sequence, str):
raise TypeError("Sequence must be list of positive integers")

if min(sequence) < 0:
raise TypeError("Sequence must be list of positive integers")
Copy link
Member

@cclauss cclauss May 21, 2020

Choose a reason for hiding this comment

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

Why not sort these things? They should work just fine.

Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

Nice!

@cclauss cclauss merged commit a15f825 into TheAlgorithms:master May 22, 2020
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* Added bead sort

* Commit suggestion

* Added checking before sort

* Bead sort only works for sequences of nonegative integers

Co-authored-by: Christian Clauss <cclauss@me.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants