sorts.reverse_selection

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

Attributes

user_input

Classes

Comparable

Base class for protocol classes.

Functions

reverse_selection_sort(→ list[T])

A pure implementation of reverse selection sort algorithm in Python

reverse_subarray(→ None)

Reverse a subarray in-place.

Module Contents

class sorts.reverse_selection.Comparable

Bases: Protocol

Base class for protocol classes.

Protocol classes are defined as:

class Proto(Protocol):
    def meth(self) -> int:
        ...

Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing).

For example:

class C:
    def meth(self) -> int:
        return 0

def func(x: Proto) -> int:
    return x.meth()

func(C())  # Passes static type check

See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as:

class GenProto[T](Protocol):
    def meth(self) -> T:
        ...
__lt__(other: Any, /) → bool
sorts.reverse_selection.reverse_selection_sort[T: Comparable](collection: list[T]) → list[T]

A pure implementation of reverse selection sort algorithm in Python

Parameters:

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"])
Traceback (most recent call last):
...
TypeError: ...
sorts.reverse_selection.reverse_subarray[T](arr: list[T], start: int, end: int) → None

Reverse a subarray in-place.

Parameters:
  • arr – the array containing the subarray to be reversed

  • start – the starting index of the subarray

  • 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]
sorts.reverse_selection.user_input