Comparing Sorting Algorithms in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Choosing a Sort

Pick a sort by input size, how sorted the data already is, stability needs, and memory limits. For tiny or nearly sorted arrays use insertion sort. For general data use qsort, merge sort (stable), or quick sort. For guaranteed O(n log n) with O(1) space use heap sort. For small integer keys consider counting or radix sort.

Remember
/* sort        time (avg)   space  stable
   bubble      O(n^2)       O(1)   yes
   selection   O(n^2)       O(1)   no
   insertion   O(n^2)       O(1)   yes
   merge       O(n log n)   O(n)   yes
   quick       O(n log n)   O(log n) no
   heap        O(n log n)   O(1)   no
   counting    O(n + k)     O(k)   yes */

Cheat Sheet

Keep these facts handy while you work with Comparing Sorting Algorithms in C in C.

TopicDetail
Tiny / nearly sortedInsertion sort
General purposeqsort or merge sort
Stable neededMerge or insertion
Small integer keysCounting or radix

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Recursion and Backtracking.

Example 1 — Count work for three simple sorts

The input is 5, 1, 4, 2, 8.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    int src[] = {5, 1, 4, 2, 8};
    int a[5], n = 5;

    memcpy(a, src, sizeof a);
    int cmps = 0, swaps = 0;
    for (int i = 0; i < n - 1; i++) {
        int swapped = 0;
        for (int j = 0; j < n - 1 - i; j++) {
            cmps++;
            if (a[j] > a[j + 1]) {
                int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t;
                swaps++;
                swapped = 1;
            }
        }
        if (!swapped) break;
    }
    printf("bubble: %d comparisons, %d swaps\n", cmps, swaps);

    memcpy(a, src, sizeof a);
    cmps = 0; swaps = 0;
    for (int i = 0; i < n - 1; i++) {
        int min = i;
        for (int j = i + 1; j < n; j++) {
            cmps++;
            if (a[j] < a[min]) min = j;
        }
        if (min != i) {
            int t = a[i]; a[i] = a[min]; a[min] = t;
            swaps++;
        }
    }
    printf("selection: %d comparisons, %d swaps\n", cmps, swaps);

    memcpy(a, src, sizeof a);
    cmps = 0; int shifts = 0;
    for (int i = 1; i < n; i++) {
        int key = a[i], j = i - 1;
        while (j >= 0) {
            cmps++;
            if (a[j] > key) { a[j + 1] = a[j]; shifts++; j--; }
            else break;
        }
        a[j + 1] = key;
    }
    printf("insertion: %d comparisons, %d shifts\n", cmps, shifts);
    return 0;
}

How It Works

  1. Selection sort always makes 4 + 3 + 2 + 1 = 10 comparisons but few swaps.
  2. Bubble sort stops after three passes (4 + 3 + 2 = 9 comparisons) because the last pass swaps nothing.
  3. Insertion sort uses the fewest comparisons here because the data is almost ordered.

Edge Cases & Pitfalls

Check these before relying on Comparing Sorting Algorithms in C in real programs.

bench

Benchmarking tiny inputs

Differences only show on larger inputs; test with realistic sizes.

own

Rolling your own

Prefer qsort for production unless you need stability or special keys.

Key Takeaways

  • Match algorithm to data.
  • Insertion for tiny or nearly sorted.
  • O(n log n) for general data.

One line: Pick a sort by input size, how sorted the data already is, stability needs, and memory limits.

Frequently Asked Questions

Use qsort for general purposes, insertion sort for tiny arrays, merge sort if you need stability, and counting or radix sort for small integer keys.
Insertion sort, because it adapts to nearly sorted data and does few operations in practice.
Continue with "C Recursion and Backtracking" next. Solve problems recursively in C: base cases, factorial, Fibonacci, Tower of Hanoi, and backtracking puzzles. The C Sorting Algorithms hub lists every topic in order.

Next: Recursion and Backtracking

Solve problems recursively in C: base cases, factorial, Fibonacci, Tower of Hanoi, and backtracking puzzles.

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