-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Added reverse_selection.py #11809
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
amarpreet2209
wants to merge
4
commits into
TheAlgorithms:master
Choose a base branch
from
amarpreet2209:fix/add-reverse_selection
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
Added reverse_selection.py #11809
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
96c5b77
Added reverse_selection.py
amarpreet2209 608e6ed
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7c59ca3
Updated reverse_selection.py
amarpreet2209 5c229b6
[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,87 @@ | ||
""" | ||
A pure Python implementation of the Reverse Selection Sort algorithm | ||
|
||
This algorithm progressively sorts the array by reversing subarrays | ||
|
||
For doctests run following command: | ||
python3 -m doctest -v reverse_selection_sort.py | ||
|
||
For manual testing run: | ||
python3 reverse_selection_sort.py | ||
""" | ||
|
||
|
||
def reverse_subarray(arr: list, start: int, end: int) -> None: | ||
""" | ||
Reverse a subarray in-place. | ||
|
||
:param arr: the array containing the subarray to be reversed | ||
:param start: the starting index of the subarray | ||
:param end: the ending index of the subarray | ||
|
||
Examples: | ||
>>> lst = [1, 2, 3, 4, 5] | ||
>>> reverse_subarray(lst, 1, 3) | ||
>>> lst | ||
[1, 4, 3, 2, 5] | ||
|
||
>>> lst = [1] | ||
>>> reverse_subarray(lst, 0, 0) | ||
>>> lst | ||
[1] | ||
|
||
>>> lst = [1, 2] | ||
>>> reverse_subarray(lst, 0, 1) | ||
>>> lst | ||
[2, 1] | ||
""" | ||
while start < end: | ||
arr[start], arr[end] = arr[end], arr[start] | ||
start += 1 | ||
end -= 1 | ||
|
||
|
||
def reverse_selection_sort(collection: list) -> list: | ||
""" | ||
A pure implementation of reverse selection sort algorithm in Python | ||
|
||
:param collection: some mutable ordered collection with heterogeneous | ||
comparable items inside | ||
:return: the same collection sorted in ascending order | ||
|
||
Examples: | ||
>>> reverse_selection_sort([1, 9, 5, 21, 17, 6]) | ||
[1, 5, 6, 9, 17, 21] | ||
|
||
>>> reverse_selection_sort([]) | ||
[] | ||
|
||
>>> reverse_selection_sort([-3, -17, -48]) | ||
[-48, -17, -3] | ||
|
||
>>> reverse_selection_sort([1, 1, 1, 1]) | ||
[1, 1, 1, 1] | ||
|
||
>>> reverse_selection_sort([5, 4, 3, 2, 1]) | ||
[1, 2, 3, 4, 5] | ||
""" | ||
n = len(collection) | ||
for i in range(n - 1): | ||
# Find the minimum element in the unsorted portion | ||
min_idx = i | ||
for j in range(i + 1, n): | ||
if collection[j] < collection[min_idx]: | ||
min_idx = j | ||
|
||
# If the minimum is not at the start of the unsorted portion, | ||
# reverse the subarray to bring it to the front | ||
if min_idx != i: | ||
reverse_subarray(collection, i, min_idx) | ||
|
||
return collection | ||
|
||
|
||
if __name__ == "__main__": | ||
user_input = input("Enter numbers separated by a comma:\n").strip() | ||
unsorted = [int(item) for item in user_input.split(",")] | ||
print(reverse_selection_sort(unsorted)) |
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
sorts/reverse_selection.py
, please provide doctest for the functionreverse_subarray
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.
Done