0
0
CComparisonBeginner · 4 min read

Bubble Sort vs Selection Sort in C: Key Differences and Code Examples

Both 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.

FactorBubble SortSelection Sort
Algorithm TypeComparison-based, swapping adjacent elementsComparison-based, selecting minimum element
Number of SwapsMany swaps (every adjacent inversion)Fewer swaps (one per pass)
Time ComplexityO(n²) average and worst caseO(n²) average and worst case
Best CaseO(n) if array is already sortedO(n²) regardless of input
StabilityStable (does not change order of equal elements)Not stable (may change order)
Use CaseGood for nearly sorted dataGood 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.

c
#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;
}
Output
11 12 22 25 34 64 90
↔️

Selection Sort Equivalent

Here is the C code for Selection Sort sorting the same integer array in ascending order.

c
#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;
}
Output
11 12 22 25 34 64 90
🎯

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.

Key Takeaways

Bubble Sort swaps adjacent elements repeatedly and is stable but can do many swaps.
Selection Sort selects the minimum element each pass, minimizing swaps but is not stable.
Both have O(n²) time complexity but differ in swap counts and stability.
Use Bubble Sort for nearly sorted data and when stability matters.
Use Selection Sort when swaps are costly and stability is not required.