forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_selection.py
More file actions
104 lines (77 loc) · 2.64 KB
/
Copy pathreverse_selection.py
File metadata and controls
104 lines (77 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
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.py
For manual testing run:
python3 reverse_selection.py
"""
from typing import Any, Protocol
class Comparable(Protocol):
def __lt__(self, other: Any, /) -> bool: ...
def reverse_subarray[T](arr: list[T], 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[T: Comparable](collection: list[T]) -> list[T]:
"""
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]
>>> reverse_selection_sort(["banana", "apple", "cherry"])
['apple', 'banana', 'cherry']
>>> reverse_selection_sort([3.14, 1.5, 2.7])
[1.5, 2.7, 3.14]
>>> reverse_selection_sort([1, "a"]) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: ...
"""
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))