C Bubble Sort

Intermediate
6 min read
Updated: Nov 2026
1 example

Bubbling the Largest to the End

Bubble sort compares adjacent pairs and swaps them if out of order. After each pass the largest remaining element has "bubbled" to its final position at the end. If a pass makes no swaps the array is sorted and the loop can stop early. It is O(n^2) average and worst case, O(n) best case with the early exit, stable and in place.

Remember
for (i = 0; i < n - 1; i++) {
    swapped = 0;
    for (j = 0; j < n - 1 - i; j++)
        if (a[j] > a[j + 1]) { swap(a[j], a[j + 1]); swapped = 1; }
    if (!swapped) break;
}

Cheat Sheet

Keep these facts handy while you work with Bubble Sort in C.

TopicDetail
BestO(n) with early exit
Average / worstO(n^2)
SpaceO(1)
StableYes

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Selection Sort.

Example 1 — Trace each pass

Sort 64, 25, 12, 22, 11.

C
#include <stdio.h>

int main(void) {
    int a[] = {64, 25, 12, 22, 11};
    int n = 5;

    for (int i = 0; i < n - 1; i++) {
        int swapped = 0;
        for (int j = 0; j < n - 1 - i; j++) {
            if (a[j] > a[j + 1]) {
                int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t;
                swapped = 1;
            }
        }
        printf("pass %d:", i + 1);
        for (int k = 0; k < n; k++) printf(" %d", a[k]);
        printf("\n");
        if (!swapped) break;
    }
    return 0;
}

How It Works

  1. Pass 1 carries 64 all the way to the end.
  2. Pass 2 carries 25 to its place before 64.
  3. Each pass shortens the inner loop because the tail is already final.

Edge Cases & Pitfalls

Check these before relying on Bubble Sort in real programs.

noexit

No early exit

Without the swapped flag, bubble sort does full work even on sorted input.

range

Inner loop bound

Use n - 1 - i so a[j + 1] never goes out of bounds.

Key Takeaways

  • Swap adjacent pairs.
  • Largest settles at the end each pass.
  • O(n^2), stable.

One line: Bubble sort compares adjacent pairs and swaps them if out of order.

Frequently Asked Questions

O(n^2) in the average and worst cases, and O(n) in the best case if you stop when a pass makes no swaps.
Yes, because it only swaps strictly out-of-order neighbors.
Continue with "C Selection Sort" next. Find the smallest remaining element and place it next. The C Sorting Algorithms hub lists every topic in order.

Next: Selection Sort

Find the smallest remaining element and place it next.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful