Bubble Sort - Complete Guide

Learn, Visualize, and Understand Bubble Sort Algorithm

What is Bubble Sort?

Bubble Sort is a simple sorting algorithm that repeatedly steps through the array, compares adjacent elements, and swaps them if they are in the wrong order. The largest elements "bubble up" to the end of the array with each pass.

Key Characteristics

  • Comparison-based: Compares adjacent elements
  • In-place sorting: Requires no extra memory
  • Stable: Maintains relative order of equal elements
  • Time Complexity: O(n²) - Inefficient for large datasets

Real-Life Example

Sorting students by height: Imagine students standing in a line.

  1. Compare first two students - if shorter student is ahead, they swap positions
  2. Move to next pair and repeat
  3. After one pass, tallest student reaches the end
  4. Repeat until all students are sorted

This is exactly how bubble sort works!

How Bubble Sort Works

Step 1: Start from the beginning of the array

Step 2: Compare adjacent elements (arr[i] and arr[i+1])

Step 3: If they're in wrong order, swap them

Step 4: Move to next pair and repeat

Step 5: After each pass, largest unsorted element reaches its correct position

Step 6: Repeat until no swaps are needed

Example Walkthrough

Array: [5, 1, 4, 2, 8]

Pass 1:

  • (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

Pass 2:

  • (1, 4) → No swap
  • (4, 2) → Swap → [1, 2, 4, 5, 8]
  • (4, 5) → No swap

Pass 3: No swaps needed → Array is sorted!

Complexity Analysis

Case Time Complexity Description
Best Case O(n) Array already sorted (optimized version)
Average Case O(n²) Elements in random order
Worst Case O(n²) Array sorted in reverse order
Space Complexity O(1) Only uses constant extra space

Advantages

  • ✅ Simple and easy to understand
  • ✅ Easy to implement
  • ✅ No extra memory required (in-place)
  • ✅ Stable sorting algorithm
  • ✅ Detects if array is already sorted

Disadvantages

  • ❌ Very slow for large datasets (O(n²))
  • ❌ Not suitable for large arrays
  • ❌ Performance much worse than advanced algorithms

When to Use Bubble Sort

  • Very small datasets (< 10 elements)
  • Educational purposes to understand sorting
  • Nearly sorted arrays (with optimization)
  • When simplicity is more important than efficiency

Interactive Bubble Sort Visualization

Watch how bubble sort compares and swaps adjacent elements to sort the array

Unsorted
Comparing
Swapping
Sorted

Bubble Sort - C Code Implementation

See the C code and watch how it executes step-by-step

C Source Code

1#include <stdio.h>
2
3void bubbleSort(int arr[], int n) {
4 for (int i = 0; i < n-1; i++) {
5 int swapped = 0;
6
7 for (int j = 0; j < n-i-1; j++) {
8 if (arr[j] > arr[j+1]) {
9 int temp = arr[j];
10 arr[j] = arr[j+1];
11 arr[j+1] = temp;
12 swapped = 1;
13 }
14 }
15
16 if (!swapped) break;
17 }
18}

Variables

arr[] -
n -
i (pass) -
j (index) -
swapped -

Console Output

Ready to execute. Click "Execute Code" to start.

Complete C Code with Comments

#include <stdio.h>

// Bubble Sort Function - Sorts array in ascending order
void bubbleSort(int arr[], int n) {
    // Outer loop - number of passes
    for (int i = 0; i < n-1; i++) {
        int swapped = 0;  // Flag to detect if swap occurred
        
        printf("Pass %d:\\n", i+1);
        
        // Inner loop - compare adjacent elements
        for (int j = 0; j < n-i-1; j++) {
            printf("  Comparing arr[%d]=%d and arr[%d]=%d\\n", 
                   j, arr[j], j+1, arr[j+1]);
            
            // If elements are in wrong order, swap them
            if (arr[j] > arr[j+1]) {
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
                swapped = 1;
                printf("  → Swapped!\\n");
            }
        }
        
        // If no swaps, array is sorted
        if (!swapped) {
            printf("No swaps in this pass. Array is sorted!\\n");
            break;
        }
        
        printf("\\n");
    }
}

int main() {
    int arr[] = {64, 34, 25, 12, 22};
    int n = sizeof(arr) / sizeof(arr[0]);
    
    printf("Original array: ");
    for(int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\\n\\n");
    
    bubbleSort(arr, n);
    
    printf("\\nSorted array: ");
    for(int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\\n");
    
    return 0;
}