Skip to content
Code it · Write it

Write bubble sort

Write bubble_sort(arr) (Python) or bubbleSort(arr) (JavaScript/TypeScript). It sorts arr in place, smallest first, and returns nothing.

Walk through the array comparing each pair of neighbours, and swap a pair when the left one is bigger. One full pass moves the biggest value to the end; repeat until a pass needs no swaps, or until every element has had its turn.

def bubble_sort(arr):
    # Sort arr in place, smallest first.
    # Compare neighbours and swap them when they are out of order.
    pass