"""Different sorting algorithms.
"""


def insertion_sort(lst: list) -> None:
    """Sort the items of lst in non-decreasing order, in place.

    >>> lst = [4, 2, 5, 6, 7, 3, 1]
    >>> insertion_sort(lst)
    >>> lst
    [1, 2, 3, 4, 5, 6, 7]
    >>> lst = [5, 2]
    >>> insertion_sort(lst)
    >>> lst
    [2, 5]
    >>> lst = [42]
    >>> insertion_sort(lst)
    >>> lst
    [42]
    >>> lst = []
    >>> insertion_sort(lst)
    >>> lst
    []
    """

    for i in range(len(lst)):
        insert(lst, i)
        # print(lst)  # uncomment to see the passes


def insert(lst: list, i: int) -> None:
    """Insert lst[i] at an index between 0 and i, so that after the insert
    lst[:i + 1] is sorted in non-decreasing order.

    Preconditions: len(lst) > 0
                   0 <= i < len(lst)
                   lst[:i] is sorted in non-descending order.

    >>> lst = [7, 3, 5, 2]
    >>> insert(lst, 1)
    >>> lst
    [3, 7, 5, 2]
    >>> insert(lst, 2)
    >>> lst
    [3, 5, 7, 2]
    >>> insert(lst, 3)
    >>> lst
    [2, 3, 5, 7]
    >>> lst = [42]
    >>> insert(lst, 0)
    >>> lst
    [42]

    """

    value = lst[i]

    while i > 0 and lst[i - 1] > value:
        lst[i] = lst[i - 1]
        i = i - 1

    lst[i] = value


def selection_sort(lst: list) -> None:
    """Sort the items of lst in non-decreasing order, in place.

    >>> lst = [4, 2, 5, 6, 7, 3, 1]
    >>> selection_sort(lst)
    >>> lst
    [1, 2, 3, 4, 5, 6, 7]
    >>> lst = [5, 2]
    >>> selection_sort(lst)
    >>> lst
    [2, 5]
    >>> lst = [42]
    >>> selection_sort(lst)
    >>> lst
    [42]
    >>> lst = []
    >>> selection_sort(lst)
    >>> lst
    []
    """

    for i in range(len(lst)):
        index_of_smallest = get_index_of_smallest(lst, i)
        lst[index_of_smallest], lst[i] = lst[i], lst[index_of_smallest]
        # print(lst)  # uncomment to see the passes


def get_index_of_smallest(lst: list, i: int) -> int:
    """Return the index of the smallest item in lst[i:].

    Preconditions: len(lst) > 0
                   0 <= i < len(lst)

    >>> get_index_of_smallest([42], 0)
    0
    >>> get_index_of_smallest([2, 7, 3, 5], 0)
    0
    >>> get_index_of_smallest([2, 7, 3, 5], 1)
    2
    >>> get_index_of_smallest([2, 7, 3, 5], 3)
    3
    """

    index_of_smallest = i

    for j in range(i + 1, len(lst)):
        if lst[j] < lst[index_of_smallest]:
            index_of_smallest = j

    return index_of_smallest


def bubble_sort(lst: list) -> None:
    """Sort the items of lst in non-decreasing order, in place.

    >>> lst = [4, 2, 5, 6, 7, 3, 1]
    >>> bubble_sort(lst)
    >>> lst
    [1, 2, 3, 4, 5, 6, 7]
    >>> lst = [5, 2]
    >>> bubble_sort(lst)
    >>> lst
    [2, 5]
    >>> lst = [42]
    >>> bubble_sort(lst)
    >>> lst
    [42]
    >>> lst = []
    >>> bubble_sort(lst)
    >>> lst
    []
    """

    for i in range(len(lst), 1, -1):
        bubble_up(lst, i)
        # print(lst)  # uncomment to see the passes


def bubble_up(lst: list, end: int) -> None:
    """Bubble up the largest element in lst[:end] into index end-1.

    Preconditions: 0 <= end <= len(lst)

    >>> lst = []
    >>> bubble_up(lst, 0)
    >>> lst
    []
    >>> lst = [42]
    >>> bubble_up(lst, 1)
    >>> lst
    [42]
    >>> lst = [4, 2, 5, 6, 7, 3, 1]
    >>> bubble_up(lst, 7)
    >>> lst
    [2, 4, 5, 6, 3, 1, 7]
    >>> lst = [4, 2, 5, 6, 7, 3, 1]
    >>> bubble_up(lst, 5)
    >>> lst
    [2, 4, 5, 6, 7, 3, 1]

    """

    for i in range(1, end):
        if lst[i - 1] > lst[i]:
            lst[i - 1], lst[i] = lst[i], lst[i - 1]  # swap!

# The swapping
# a, b = b, a
# can be used in Python as a shortcut for the traditional
# temp = a
# a = b
# b = temp


if __name__ == '__main__':
    import doctest
    doctest.testmod()
