C Program for Bubble Sort with Example and Explanation
for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; }}}.Examples
How to Think About It
Algorithm
Code
#include <stdio.h> void bubbleSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } int main() { int arr[] = {5, 3, 8, 4, 2}; int n = sizeof(arr) / sizeof(arr[0]); bubbleSort(arr, n); printf("Sorted array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Dry Run
Let's trace the array {5, 3, 8, 4, 2} through the bubble sort code.
First pass comparisons and swaps
Compare 5 and 3, swap -> {3, 5, 8, 4, 2}; compare 5 and 8, no swap; compare 8 and 4, swap -> {3, 5, 4, 8, 2}; compare 8 and 2, swap -> {3, 5, 4, 2, 8}
Second pass comparisons and swaps
Compare 3 and 5, no swap; compare 5 and 4, swap -> {3, 4, 5, 2, 8}; compare 5 and 2, swap -> {3, 4, 2, 5, 8}
Third pass comparisons and swaps
Compare 3 and 4, no swap; compare 4 and 2, swap -> {3, 2, 4, 5, 8}
Fourth pass comparisons and swaps
Compare 3 and 2, swap -> {2, 3, 4, 5, 8}
| Pass | Array State |
|---|---|
| 1 | 3 5 4 2 8 |
| 2 | 3 4 2 5 8 |
| 3 | 3 2 4 5 8 |
| 4 | 2 3 4 5 8 |
Why This Works
Step 1: Nested loops for passes and comparisons
The outer loop controls how many passes we make, and the inner loop compares adjacent elements to swap if needed.
Step 2: Swapping elements
When the left element is bigger than the right, swapping moves the bigger element towards the end.
Step 3: Reducing comparisons each pass
Each pass places the largest unsorted element at the end, so the inner loop shortens by one each time.
Alternative Approaches
#include <stdio.h> void bubbleSortOptimized(int arr[], int n) { int swapped; for (int i = 0; i < n - 1; i++) { swapped = 0; for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; swapped = 1; } } if (!swapped) break; } } int main() { int arr[] = {5, 3, 8, 4, 2}; int n = sizeof(arr) / sizeof(arr[0]); bubbleSortOptimized(arr, n); printf("Sorted array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
#include <stdio.h> void selectionSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { int min_idx = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[min_idx]) { min_idx = j; } } int temp = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = temp; } } int main() { int arr[] = {5, 3, 8, 4, 2}; int n = sizeof(arr) / sizeof(arr[0]); selectionSort(arr, n); printf("Sorted array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Complexity: O(n²) time, O(1) space
Time Complexity
Bubble sort uses two nested loops over the array, so it takes about n×n = n² steps in the worst and average cases.
Space Complexity
It sorts the array in place, so it only needs a few extra variables, making space complexity constant O(1).
Which Approach is Fastest?
Optimized bubble sort can be faster on nearly sorted data by stopping early, but selection sort reduces swaps, which can be better in some cases.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Bubble Sort | O(n²) | O(1) | Simple, small arrays |
| Optimized Bubble Sort | O(n) best, O(n²) worst | O(1) | Nearly sorted arrays |
| Selection Sort | O(n²) | O(1) | When swaps are costly |