C in 100 Seconds: Bubble Sort
Video: C in 100 Seconds: Bubble Sort | Episode 53 by CelesteAI
Watch full page →Bubble Sort
The simplest sorting algorithm. Compare two adjacent elements — if the left one is bigger, swap them. Do that for every pair, and the largest value bubbles to the end. Repeat until sorted.
How It Works
Two nested loops. The outer loop controls how many passes — each pass pushes one more element into its final position. The inner loop walks through the unsorted portion, comparing neighbors.
The Swap
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
Output
Before: 64 25 12 22 11
After: 11 12 22 25 64
Complexity
O(n squared) — for every element, it potentially compares against every other element. Not efficient for large arrays, but perfectly fine for small ones.
Student code: GitHub