Skip to content

added powersort in sorts/power_sort.py - #13219

Merged
cclauss merged 11 commits into
TheAlgorithms:masterfrom
a3ro-dev:master
Sep 9, 2026
Merged

added powersort in sorts/power_sort.py#13219
cclauss merged 11 commits into
TheAlgorithms:masterfrom
a3ro-dev:master

Conversation

@a3ro-dev

@a3ro-dev a3ro-dev commented Oct 5, 2025

Copy link
Copy Markdown
Contributor

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • 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 include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@a3ro-dev
a3ro-dev requested a review from cclauss as a code owner October 5, 2025 10:10
@algorithms-keeper algorithms-keeper Bot added the tests are failing Do not merge until tests pass label Oct 5, 2025
…ted import statements, removed unnecessary whitespace, and enhanced comments for better readability. Adjusted key function handling for reverse sorting and ensured proper handling of numeric and non-numeric types.
@a3ro-dev

a3ro-dev commented Oct 5, 2025

Copy link
Copy Markdown
Contributor Author

ruff issues fixed

@algorithms-keeper algorithms-keeper Bot added require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html labels Oct 5, 2025

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Comment thread sorts/power_sort.py Outdated
return start + 1


key_func = key if key else lambda x: x

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide descriptive name for the parameter: x

Comment thread sorts/power_sort.py Outdated
return run_end


def _node_power(n: int, b1: int, n1: int, b2: int, n2: int) -> int:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide descriptive name for the parameter: n

Comment thread sorts/power_sort.py Outdated
>>> arr
[1, 2, 3, 5, 6, 7]
"""
key_func = key if key else lambda x: x

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide descriptive name for the parameter: x

Comment thread sorts/power_sort.py Outdated
if key:
original_key = key

def reverse_key(x):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file sorts/power_sort.py, please provide doctest for the function reverse_key

Please provide return type hint for the function: reverse_key. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x

Comment thread sorts/power_sort.py Outdated
needs_final_reverse = True
else:

def reverse_cmp(x):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file sorts/power_sort.py, please provide doctest for the function reverse_cmp

Please provide return type hint for the function: reverse_cmp. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x

@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Oct 5, 2025
@algorithms-keeper algorithms-keeper Bot removed the tests are failing Do not merge until tests pass label Oct 5, 2025
…rameter names for better understanding, improved comments for key functions, and ensured consistent handling of total length in calculations. This improves readability and maintainability of the code.
@algorithms-keeper algorithms-keeper Bot removed require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html labels Oct 5, 2025
@algorithms-keeper algorithms-keeper Bot added the tests are failing Do not merge until tests pass label Oct 5, 2025
…ility. Adjusted whitespace and line breaks in function definitions and comments to enhance clarity without altering functionality.
@algorithms-keeper algorithms-keeper Bot removed the tests are failing Do not merge until tests pass label Oct 5, 2025
@a3ro-dev

a3ro-dev commented Oct 5, 2025

Copy link
Copy Markdown
Contributor Author

mhmm??

@a3ro-dev

a3ro-dev commented Oct 6, 2025

Copy link
Copy Markdown
Contributor Author

@cclauss hello?

Copilot AI lite review requested due to automatic review settings September 9, 2026 12:29
@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Sep 9, 2026
@cclauss
cclauss merged commit bbb0940 into TheAlgorithms:master Sep 9, 2026
5 checks passed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Reverse sorting has correctness/stability issues (especially with key) and should be fixed before approval.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds a new power_sort implementation to the sorts/ module, providing an adaptive merge sort variant based on the PowerSort merging strategy.

Changes:

  • Introduces run detection (_find_run), node-power calculation (_node_power), and merging (_merge) helpers.
  • Implements power_sort() with optional key and reverse parameters and extensive doctest examples.
  • Adds a __main__ entrypoint for doctest and interactive manual testing.
File summaries
File Description
sorts/power_sort.py New PowerSort-based sorting implementation with helper functions, doctests, and a manual test harness.
Review details

Suppressed comments (1)

sorts/power_sort.py:302

  • reverse_cmp() is a nested function, so its docstring doctests won’t be discovered by doctest.testmod(). Removing the docstring (or moving the helper to module scope) avoids implying there is doctest coverage here.
            def reverse_cmp(element: Any) -> Any:
                """
                Reverse comparison function for numeric values.

                Args:
  • Files reviewed: 1/1 changed files
  • Comments generated: 4
  • Review effort level: Lite

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread sorts/power_sort.py
Comment on lines +379 to +390
# Handle reverse sorting for non-numeric types
if (
reverse
and needs_final_reverse
and key
and len(arr) > 0
and not isinstance(arr[0], int | float)
):
# For non-numeric types, we need to reverse the final result
# Check if we used numeric negation or not
arr.reverse()

Comment thread sorts/power_sort.py
Comment on lines +268 to +272
# Adjust key function for reverse sorting
needs_final_reverse = False
if reverse:
if key:
original_key = key
Comment thread sorts/power_sort.py
Comment on lines +218 to +220

Returns:
The same collection ordered according to the parameters
Comment thread sorts/power_sort.py
Comment on lines +274 to +293
def reverse_key(element: Any) -> Any:
"""
Reverse key function for numeric values.

Args:
element: The element to process

Returns:
Negated value for numeric types, original value otherwise

>>> reverse_key(5)
-5
>>> reverse_key('hello')
'hello'
"""
val = original_key(element)
if isinstance(val, int | float):
return -val
return val

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.

3 participants