Bubble Sort

def bubble_sort(collection):

    def swap(i, j):
				tmp = collection[j]
        collection[j] = collection[i]
				collection[i] = tmp

    n = len(collection)
    swapped = True
    x = -1

    while swapped:
        swapped = False
        x += 1

        for i in range(1, n - x):
            if collection[i - 1] > collection[i]:
                swap(i - 1, i)
                swapped = True
    return collection

Complexity: O(n2)O(n^2)

What is bubble sort?

Sorting algorithm that iterates through a list, compares adjacent elements and swaps elements if they are in the wrong order