Unit 5 of 12 · Sorting

Bubble Sort

1 min Updated Jul 2026

Bubble sort is one of the simplest sorting algorithms. It steps through the list (we refer to it as a pass), compares adjacent elements, and swaps them if they are in the wrong order. After each full pass through the array, the largest unsorted element is guaranteed to be in its correct final position. So with every pass, we need to check one fewer element. If you didn’t get the previous jumbo mumbo, no worries! The example is right below.

Step-by-Step Example

Let’s sort the following array from smallest to largest:

Pass 1

We compare each adjacent pair. Yellow = comparing, red = just swapped.

Pass 2

Pass 3

Pass 4

✅ Final sorted array:

Try it yourself!

Bubble sort step-by-step animator showing each comparison and swap as colour-coded bars

Press next to begin Pass 0
Comparing
Swapped
Sorted
Step 0 / 0
Sorted! All elements are in their final positions.

Enter up to 20 comma-separated numbers.

Important note!

Bubble sort is easy to implement and understand. However, it performs poorly on large arrays/datasets. Therefore, bubble sort is usually only used in a learning context and not to solve real world problems. You can use faster algorithms like Merge Sort and Quick Sort.