-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Create reversesort #11886
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
base: master
Are you sure you want to change the base?
Create reversesort #11886
Conversation
Time Complexity: The time complexity of this algorithm is still O(n²), similar to traditional selection sort, since it involves finding the minimum element in the unsorted portion and reversing a subarray. Unique Feature: The reversal of subarrays distinguishes this algorithm from traditional selection sort, providing an interesting approach to sorting.
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.
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.
data_structures/reversesort.py
Outdated
@@ -0,0 +1,29 @@ | |||
def reverse_selection_sort(arr): |
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.
As there is no test file in this pull request nor any test function or class in the file data_structures/reversesort.py
, please provide doctest for the function reverse_selection_sort
Please provide return type hint for the function: reverse_selection_sort
. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide type hint for the parameter: arr
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.
Type hinting: For the function parameter arr and the return type.
Doctests: To validate the functionality.
Updated Code with Type Hinting and Doctests:
from typing import List
def reverse_selection_sort(arr: List[int]) -> List[int]:
"""
Sorts an array using a modified selection sort algorithm where after finding
the minimum element, a subarray is reversed instead of swapping.
Parameters:
arr (List[int]): The list of integers to sort.
Returns:
List[int]: The sorted list of integers.
Example:
>>> reverse_selection_sort([64, 25, 12, 22, 11])
[11, 12, 22, 25, 64]
>>> reverse_selection_sort([5, 4, 3, 2, 1])
[1, 2, 3, 4, 5]
>>> reverse_selection_sort([3, 1, 2])
[1, 2, 3]
>>> reverse_selection_sort([10])
[10]
"""
n = len(arr)
# Iterate over each position of the array
for i in range(n - 1):
# Find the index of the minimum element in the unsorted portion
min_index = i
for j in range(i + 1, n):
if arr[j] < arr[min_index]:
min_index = j
# If the minimum is not already at position i, reverse the subarray
if min_index != i:
# Reverse the subarray from position i to min_index
arr[i:min_index + 1] = reversed(arr[i:min_index + 1])
return arr
if name == "main":
import doctest
doctest.testmod()
Explanation:
Type Hinting:
The parameter arr is typed as List[int] to indicate that it expects a list of integers.
The return type is List[int], as the function returns the sorted list.
Doctests:
Doctests are added inside the docstring of the function to provide examples and automatically test the function.
Example inputs and expected outputs are provided in the docstring using >>>.
If you run this code, the doctest module will check if the output matches the expected results from the examples.
How to Run the Code:
You can run the script as is, and it will automatically run the tests using doctest. If all tests pass, there will be no output. If any test fails, doctest will show which test failed.
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
Time Complexity: The time complexity of this algorithm is still O(n²), similar to traditional selection sort, since it involves finding the minimum element in the unsorted portion and reversing a subarray.
Unique Feature: The reversal of subarrays distinguishes this algorithm from traditional selection sort, providing an interesting approach to sorting.
Describe your change:
Explanation:
Find the Minimum: For each position i in the array, find the minimum element in the unsorted portion.
Reverse Subarray: After finding the minimum element, reverse the subarray starting from i up to the position of the minimum element (min_index).
Repeat: This process repeats for each subsequent position, progressively sorting the array.
Checklist: