Bubble Sort

Algorithms
principle

Also known as: bubblesort

Grade 6-8

View on concept map

A simple sorting algorithm that repeatedly walks through the list, compares each pair of adjacent elements, and swaps them if they are in the wrong order. Bubble sort is easy to understand and implement, making it an excellent teaching example for understanding how sorting works.

Definition

A simple sorting algorithm that repeatedly walks through the list, compares each pair of adjacent elements, and swaps them if they are in the wrong order. This process repeats until no more swaps are needed, meaning the list is fully sorted.

πŸ’‘ Intuition

Heavier bubbles sink and lighter bubbles rise β€” larger values slowly move to the end of the list.

🎯 Core Idea

Each pass moves the largest unsorted element to its correct position at the end.

Example

[5,3,1]: compare 5,3 β†’ swap β†’ [3,5,1]; compare 5,1 β†’ swap β†’ [3,1,5]; repeat until sorted.

Formula

O(n^2) time complexity

🌟 Why It Matters

Bubble sort is easy to understand and implement, making it an excellent teaching example for understanding how sorting works. However, its O(n^2) performance makes it impractical for large datasets, which is why more efficient algorithms like merge sort are used in practice.

πŸ’­ Hint When Stuck

To trace bubble sort, make repeated passes through the list. On each pass, compare every pair of adjacent elements and swap if the left is greater. After each pass, the largest unsorted value is in its final position. Stop when a full pass makes no swaps.

Formal View

Bubble sort performs up to n-1 passes over array A[0..n-1]. In each pass, for j = 0 to n-2-i, if A[j] > A[j+1], swap them. After pass i, the i+1 largest elements are in their final positions. Worst and average case: O(n^2).

🚧 Common Stuck Point

Bubble sort is O(nΒ²) β€” avoid it for large data; use merge sort or quick sort instead.

⚠️ Common Mistakes

  • Not optimizing with an early exitβ€”if a pass makes no swaps, the list is already sorted and you can stop
  • Comparing elements beyond the sorted portion at the end of the array, wasting comparisons
  • Using bubble sort for large datasets when O(n \log n) algorithms are available and dramatically faster

Frequently Asked Questions

What is Bubble Sort in CS Thinking?

A simple sorting algorithm that repeatedly walks through the list, compares each pair of adjacent elements, and swaps them if they are in the wrong order. This process repeats until no more swaps are needed, meaning the list is fully sorted.

What is the Bubble Sort formula?

O(n^2) time complexity

When do you use Bubble Sort?

To trace bubble sort, make repeated passes through the list. On each pass, compare every pair of adjacent elements and swap if the left is greater. After each pass, the largest unsorted value is in its final position. Stop when a full pass makes no swaps.

Next Steps

How Bubble Sort Connects to Other Ideas

To understand bubble sort, you should first be comfortable with sorting, iteration and efficiency. Once you have a solid grasp of bubble sort, you can move on to merge sort.