An implementation of Bubble sort in Python.

from typing import List

def bubble_sort(ints: List[int]) -> List[int]:
    def do_pass() -> bool:
        sorted = True

        for i in range(len(ints)-1):
            if ints[i] > ints[i + 1]:
                sorted = False
                swap(i, i+1)

        return sorted

    def swap(indexA: int, indexB: int) -> None:
        valueA = ints[indexA]
        ints[indexA] = ints[indexB]
        ints[indexB] = valueA

    sorted = False
    while not sorted:
        sorted = do_pass()

    return ints

return bubble_sort([10, 5, 4, 40])

Backlinks