Sorting

Bubble Sort

What is Bubble Sort?

Bubble Sort walks through the array from one end to the other, checking each pair of neighboring values as it goes and swapping them whenever they're in the wrong order. One full walkthrough is called a pass, and the array keeps getting passed over again until a pass finishes without needing a single swap. The name comes from how the largest unsorted value rises toward its correct position with every pass, similar to a bubble floating up.

How Does It Work?

Imagine you have an unsorted list of numbers: [5, 1, 4, 2, 8]

  1. First Pass:
    • (5, 1) → Swap → [1, 5, 4, 2, 8]
    • (5, 4) → Swap → [1, 4, 5, 2, 8]
    • (5, 2) → Swap → [1, 4, 2, 5, 8]
    • (5, 8) → No swap
    5011422384105142238410415223841041225384
  2. Second Pass:
    • (1, 4) → No swap
    • (4, 2) → Swap → [1, 2, 4, 5, 8]
    • (4, 5) → No swap
    104122538410412253841021425384
  3. Third Pass:
    • No swaps needed → List is sorted
    1021425384102142538410214253841021425384
Compared & swappedCompared, no swapSorted position

The algorithm stops when a complete pass is made without any swaps.

Algorithm Steps

  1. Start with an unsorted array
  2. Set a flag to track if any swaps occur
  3. For each pair of adjacent elements:
    • Compare the two elements
    • If they are in the wrong order, swap them
    • Set the swap flag to true
  4. Repeat the process until a complete pass is made without any swaps
  5. The array is now sorted

Time Complexity

  • Best Case: Array is already sorted → O(n) (only one pass needed).
  • Average Case: Randomly ordered array → O(n²).
  • Worst Case: Array is sorted in reverse order → O(n²).

Time Complexity Analysis

Advertisement

Space Complexity

Bubble Sort is an in-place sorting algorithm, meaning it requires only O(1) additional space (for temporary storage during swaps).

Bubble Sort is simple to understand and implement but inefficient for large datasets. It's mainly used for educational purposes to introduce sorting algorithms. In practice, more efficient algorithms like QuickSort or MergeSort are preferred.

Watch Bubble Sort in action as it repeatedly swaps adjacent elements to sort the array step by step.

Speed:1x
Comparisons:
0
Swaps:
0

Array Visualization

Generate or enter an array to begin

Test Your Knowledge before moving forward!

Bubble Sort Quiz Challenge

How it works:

  • +1 point for each correct answer
  • 0 points for wrong answers
  • -0.5 point penalty for viewing explanations
  • Earn stars based on your final score (max 5 stars)

Bubble Sort Implementation

// Bubble Sort in JavaScript
function bubbleSort(arr) {
  let n = arr.length;
  
  // Outer loop for passes
  for (let i = 0; i < n - 1; i++) {
    // Inner loop for comparisons
    for (let j = 0; j < n - i - 1; j++) {
      // Swap if current element is greater than next
      if (arr[j] > arr[j + 1]) {
        // ES6 destructuring assignment for swap
        [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
      }
    }
  }
  return arr;
}

// Usage example
const unsortedArray = [64, 34, 25, 12, 22, 11, 90];
console.log("Unsorted array:", unsortedArray);
const sortedArray = bubbleSort(unsortedArray);
console.log("Sorted array:", sortedArray);

Done With the Learning

Mark Bubble Sort as done and view it on your dashboard