Bubble Sort vs Selection Sort in C: Key Differences and Code Examples
Bubble Sort and Selection Sort are simple sorting algorithms in C that sort arrays by comparing elements. Bubble Sort repeatedly swaps adjacent elements to 'bubble' the largest values to the end, while Selection Sort selects the smallest element and swaps it with the current position. Bubble Sort usually performs more swaps, making Selection Sort more efficient in that aspect.Quick Comparison
Here is a quick side-by-side comparison of Bubble Sort and Selection Sort based on key factors.
| Factor | Bubble Sort | Selection Sort |
|---|---|---|
| Algorithm Type | Comparison-based, swapping adjacent elements | Comparison-based, selecting minimum element |
| Number of Swaps | Many swaps (every adjacent inversion) | Fewer swaps (one per pass) |
| Time Complexity | O(n²) average and worst case | O(n²) average and worst case |
| Best Case | O(n) if array is already sorted | O(n²) regardless of input |
| Stability | Stable (does not change order of equal elements) | Not stable (may change order) |
| Use Case | Good for nearly sorted data | Good when swaps are costly |
Key Differences
Bubble Sort works by repeatedly stepping through the list, comparing adjacent elements and swapping them if they are in the wrong order. This causes the largest unsorted element to 'bubble up' to its correct position at the end of the array each pass. It continues until no swaps are needed, which means the array is sorted.
Selection Sort divides the array into a sorted and unsorted part. It repeatedly finds the smallest element from the unsorted part and swaps it with the first unsorted element, growing the sorted part by one each time. Unlike Bubble Sort, it does fewer swaps but still compares all unsorted elements each pass.
Because Bubble Sort swaps every time it finds an out-of-order pair, it can be slower when swaps are expensive. Selection Sort minimizes swaps but always does the same number of comparisons. Bubble Sort is stable, preserving the order of equal elements, while Selection Sort is not stable because swapping can reorder equal elements.
Code Comparison
Here is the C code for Bubble Sort sorting an integer array in ascending order.
#include <stdio.h> void bubbleSort(int arr[], int n) { int i, j, temp; for (i = 0; i < n - 1; i++) { int swapped = 0; for (j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; swapped = 1; } } if (!swapped) { break; // Array is sorted } } } int main() { int arr[] = {64, 34, 25, 12, 22, 11, 90}; int n = sizeof(arr) / sizeof(arr[0]); bubbleSort(arr, n); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Selection Sort Equivalent
Here is the C code for Selection Sort sorting the same integer array in ascending order.
#include <stdio.h> void selectionSort(int arr[], int n) { int i, j, min_idx, temp; for (i = 0; i < n - 1; i++) { min_idx = i; for (j = i + 1; j < n; j++) { if (arr[j] < arr[min_idx]) { min_idx = j; } } if (min_idx != i) { temp = arr[i]; arr[i] = arr[min_idx]; arr[min_idx] = temp; } } } int main() { int arr[] = {64, 34, 25, 12, 22, 11, 90}; int n = sizeof(arr) / sizeof(arr[0]); selectionSort(arr, n); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
When to Use Which
Choose Bubble Sort when you expect the array to be nearly sorted already, as it can finish early and is stable, preserving the order of equal elements. It is also easier to implement and understand for beginners.
Choose Selection Sort when the cost of swapping elements is high, since it minimizes swaps by only swapping once per pass. However, it always performs the same number of comparisons regardless of input order and is not stable.
For large or performance-critical applications, consider more efficient algorithms like Quick Sort or Merge Sort instead.