sorts.gnome_sort

Gnome Sort Algorithm (A.K.A. Stupid Sort)

This algorithm iterates over a list comparing an element with the previous one. If order is not respected, it swaps element backward until order is respected with previous element. It resumes the initial iteration from element new position.

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

For manual testing run: python3 gnome_sort.py

Attributes

user_input

Classes

Comparable

Base class for protocol classes.

Functions

gnome_sort(→ list[T])

Pure implementation of the gnome sort algorithm in Python

Module Contents

class sorts.gnome_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: object, /) bool
sorts.gnome_sort.gnome_sort[T: Comparable](lst: list[T]) list[T]

Pure implementation of the gnome sort algorithm in Python

Take some mutable ordered collection with heterogeneous comparable items inside as arguments, return the same collection ordered by ascending.

Examples: >>> gnome_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5]

>>> gnome_sort([])
[]
>>> gnome_sort([-2, -5, -45])
[-45, -5, -2]
>>> "".join(gnome_sort(list(set("Gnomes are stupid!"))))
' !Gadeimnoprstu'
sorts.gnome_sort.user_input