Skip to content

Added implementation for MSD radix sort algorithm based on binary representation#4441

Merged
l3str4nge merged 5 commits intoTheAlgorithms:masterfrom
Txbias:master
May 20, 2021
Merged

Added implementation for MSD radix sort algorithm based on binary representation#4441
l3str4nge merged 5 commits intoTheAlgorithms:masterfrom
Txbias:master

Conversation

@Txbias
Copy link
Copy Markdown
Contributor

@Txbias Txbias commented May 19, 2021

Describe your change:

Added implementation for the MSD radix sort algorithm. The sorting is based on the binary
representation of the numbers.

  • 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}.

@Txbias Txbias requested a review from l3str4nge as a code owner May 19, 2021 14:48
@ghost ghost added the awaiting reviews This PR is ready to be reviewed label May 19, 2021
Comment thread sorts/msd_radix_sort.py Outdated
>>> _msd_radix_sort([10, 4, 12], 2)
[4, 12, 10]
"""
if bit_position == 0 or len(list_of_ints) == 1 or len(list_of_ints) == 0:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Calling len() two times here is inefficient. Let's imagine 1kk list :)

len(list_of_ints) in (0, 1)

Comment thread sorts/msd_radix_sort.py Outdated
zeros = list()
ones = list()
# Split numbers based on bit at bit_position from the right
for index, number in enumerate(list_of_ints):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

index never used so go without enumerate

Comment thread sorts/msd_radix_sort.py
return []

if min(list_of_ints) < 0:
raise ValueError("All numbers must be positive")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add test for this assertion.

@ghost ghost added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels May 20, 2021
@ghost ghost added awaiting reviews This PR is ready to be reviewed and removed awaiting changes A maintainer has requested changes to this PR labels May 20, 2021
@Txbias
Copy link
Copy Markdown
Contributor Author

Txbias commented May 20, 2021

Hey,
thank you for your feedback.
I fixed the issues you mentioned.

Copy link
Copy Markdown
Member

@l3str4nge l3str4nge left a comment

Choose a reason for hiding this comment

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

Nice

@ghost ghost removed the awaiting reviews This PR is ready to be reviewed label May 20, 2021
@l3str4nge l3str4nge merged commit b11e531 into TheAlgorithms:master May 20, 2021
shermanhui pushed a commit to shermanhui/Python that referenced this pull request Oct 22, 2021
…resentation (TheAlgorithms#4441)

* Added MSD radix sort algorithm

* Fixed typos

* Added doctests

* Added link to wikipedia

* Added doctest and improved code
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