sorts.quick_sort

A pure Python implementation of the quick sort algorithm

For doctests run following command: python3 -m doctest -v quick_sort.py

For manual testing run: python3 quick_sort.py

Attributes

user_input

Classes

Comparable

Base class for protocol classes.

Functions

quick_sort(→ list[T])

A pure Python implementation of quicksort algorithm.

Module Contents

class sorts.quick_sort.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.quick_sort.quick_sort[T: Comparable](collection: list[T]) list[T]

A pure Python implementation of quicksort algorithm.

Parameters:

collection – a mutable collection of comparable items

Returns:

the same collection ordered in ascending order

Examples: >>> quick_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] >>> quick_sort([]) [] >>> quick_sort([-2, 5, 0, -45]) [-45, -2, 0, 5] >>> quick_sort([“z”, “a”, “m”, “b”]) [‘a’, ‘b’, ‘m’, ‘z’] >>> quick_sort([3.14, -1.0, 2.71]) [-1.0, 2.71, 3.14] >>> quick_sort([0, 5, 3, 2, 2]) == sorted([0, 5, 3, 2, 2]) True >>> quick_sort([“z”, “a”, “m”]) == sorted([“z”, “a”, “m”]) True

sorts.quick_sort.user_input