Skip to content

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
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions data_structures/reversesort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def reverse_selection_sort(arr):

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

Copy link
Author

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.

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.

Check failure on line 27 in data_structures/reversesort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/reversesort.py:27:89: E501 Line too long (103 > 88)
# Reverse Subarray: After finding the minimum element, reverse the subarray starting from i up to the position of the minimum element (min_index).

Check failure on line 28 in data_structures/reversesort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/reversesort.py:28:89: E501 Line too long (146 > 88)
# Repeat: This process repeats for each subsequent position, progressively sorting the array.

Check failure on line 29 in data_structures/reversesort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/reversesort.py:29:89: E501 Line too long (93 > 88)
Loading