Placeholder image

Bubble Sort is a simple in-place sorting algorithm, with an O(n^2 ) time complexity on worst and average case (it is quite an inefficient algorithm when n is large).

The general steps are very straight forward. The visualization operates as follows: We iterate through the array and compare each adjacent pair of elements at index i (orange) and j=i+1 (blue). If the value at i is less than the value at j, then both elements change to light green to indicate that they will be swapped, then we swap the elements.

So we compare the values of the elements in index i=0 and j=1, and if the value at index i is greater than the value at index j, then we swap the two elements. Next we compare the values of the elements in index i=1 and j=2, and if the value at index i is greater than the value at index j, then we swap the two elements.

It continues in this pattern until the largest element is swapped into the last element of the array (which begins the sorted portion of the array), and we continue this comparison process again on the unsorted portion of the array.

Bubble Sort gets its name because visually, the largest element appears to “bubble” up to the end of the array.
Color meaning:
Orange = left element to compare
Light Blue = right element to compare
Green = Pair of elements to be swapped
Dark Blue = Element in final sorted position