-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
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
Open
Shwetasingh77
wants to merge
7
commits into
TheAlgorithms:master
Choose a base branch
from
Shwetasingh77:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Create reversesort #11886
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
31a3bf3
Create reversesort
Shwetasingh77 e6dca30
Rename reversesort to reversesort.py
Shwetasingh77 c376bba
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a3e3e3d
Update reversesort.py
Shwetasingh77 ee58b59
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 720caf7
Update reversesort.py
Shwetasingh77 e735102
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
def reverse_selection_sort(arr): | ||
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 | ||
|
||
# Example usage: | ||
arr = [64, 25, 12, 22, 11] | ||
sorted_arr = reverse_selection_sort(arr) | ||
print("Sorted array:", sorted_arr) | ||
|
||
# Sorted array: [11, 12, 22, 25, 64] | ||
|
||
# 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. | ||
Oops, something went wrong.
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.
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 functionreverse_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.
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.