Skip to content

Commit ee4c04e

Browse files
Added reverse_selection.py (#11809)
* Added reverse_selection.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Updated reverse_selection.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 195ad61 commit ee4c04e

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

sorts/reverse_selection.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"""
2+
A pure Python implementation of the Reverse Selection Sort algorithm
3+
4+
This algorithm progressively sorts the array by reversing subarrays
5+
6+
For doctests run following command:
7+
python3 -m doctest -v reverse_selection_sort.py
8+
9+
For manual testing run:
10+
python3 reverse_selection_sort.py
11+
"""
12+
13+
14+
def reverse_subarray(arr: list, start: int, end: int) -> None:
15+
"""
16+
Reverse a subarray in-place.
17+
18+
:param arr: the array containing the subarray to be reversed
19+
:param start: the starting index of the subarray
20+
:param end: the ending index of the subarray
21+
22+
Examples:
23+
>>> lst = [1, 2, 3, 4, 5]
24+
>>> reverse_subarray(lst, 1, 3)
25+
>>> lst
26+
[1, 4, 3, 2, 5]
27+
28+
>>> lst = [1]
29+
>>> reverse_subarray(lst, 0, 0)
30+
>>> lst
31+
[1]
32+
33+
>>> lst = [1, 2]
34+
>>> reverse_subarray(lst, 0, 1)
35+
>>> lst
36+
[2, 1]
37+
"""
38+
while start < end:
39+
arr[start], arr[end] = arr[end], arr[start]
40+
start += 1
41+
end -= 1
42+
43+
44+
def reverse_selection_sort(collection: list) -> list:
45+
"""
46+
A pure implementation of reverse selection sort algorithm in Python
47+
48+
:param collection: some mutable ordered collection with heterogeneous
49+
comparable items inside
50+
:return: the same collection sorted in ascending order
51+
52+
Examples:
53+
>>> reverse_selection_sort([1, 9, 5, 21, 17, 6])
54+
[1, 5, 6, 9, 17, 21]
55+
56+
>>> reverse_selection_sort([])
57+
[]
58+
59+
>>> reverse_selection_sort([-3, -17, -48])
60+
[-48, -17, -3]
61+
62+
>>> reverse_selection_sort([1, 1, 1, 1])
63+
[1, 1, 1, 1]
64+
65+
>>> reverse_selection_sort([5, 4, 3, 2, 1])
66+
[1, 2, 3, 4, 5]
67+
"""
68+
n = len(collection)
69+
for i in range(n - 1):
70+
# Find the minimum element in the unsorted portion
71+
min_idx = i
72+
for j in range(i + 1, n):
73+
if collection[j] < collection[min_idx]:
74+
min_idx = j
75+
76+
# If the minimum is not at the start of the unsorted portion,
77+
# reverse the subarray to bring it to the front
78+
if min_idx != i:
79+
reverse_subarray(collection, i, min_idx)
80+
81+
return collection
82+
83+
84+
if __name__ == "__main__":
85+
user_input = input("Enter numbers separated by a comma:\n").strip()
86+
unsorted = [int(item) for item in user_input.split(",")]
87+
print(reverse_selection_sort(unsorted))

0 commit comments

Comments
 (0)