Bubble Sort

Repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. Performs poorly in real world use.

Bubble Sort Visualization

Use the options below to configure the bubble sort. Please note that - as mentioned above - bubble sort performs poorly in real world use, and will take a very long time to sort any array with number of elements greater than approximately 50.

100

Bubble Sort

An implementation of the bubble sort algorithm. Using bubble sort, we repeatedly step through the list to sort, comparing adjacent elements and swapping them if they are in the wrong order.

1 2from typing import List 3 4 5def bubble_sort(array: List[int]): 6 """ 7 Performs a bubble sort on the array. 8 :param array: is the array to sort. 9 """ 10 for i in range(len(array) - 1): 11 for j in range(len(array) - i - 1): 12 if array[j + 1] < array[j]: 13 swap(array, j + 1, j) 14 15 16def swap(array: List[int], first: int, second: int): 17 """ 18 Swaps two items in an array. 19 :param array: is the array to swap items in. 20 :param first: is the index of the first item. 21 :param second: is the index of the second item. 22 """ 23 temp = array[first] 24 array[first] = array[second] 25 array[second] = temp