C Sorting Algorithms

Intermediate
18 min read
Updated: Nov 2026
1 example
12 guides

Definition and Usage

Sorting arranges items in order so that searching, merging, and grouping become faster. Simple sorts (bubble, selection, insertion) are easy to write and run in O(n^2). Divide-and-conquer and heap-based sorts (merge, quick, heap) run in O(n log n). Non-comparison sorts (counting, bucket, radix) can reach linear time for restricted keys. This section walks through each algorithm with a traced example and ends with a comparison.

Trace small arrays by hand.

Print the array after each pass. Watching five numbers move teaches a sort faster than reading code.

Learning Path

Work through the stages in order, or jump to a topic from the index below (12 tutorials).

StageTopics
1. Simple sortsSorting basics, Bubble sort, Selection sort, Insertion sort
2. Efficient comparison sortsMerge sort, Quick sort, Heap sort, Shell sort
3. Non-comparison sortsCounting sort, Bucket sort, Radix sort
4. ComparisonSorting comparisons

C Examples

A quick taste of sorting. Compile with gcc file.c -std=c11 -Wall -o out.

Example 1 — Sort three numbers with swaps

Compare and swap until ordered.

C
#include <stdio.h>

static void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; }

int main(void) {
    int a = 3, b = 1, c = 2;

    if (a > b) swap(&a, &b);
    if (b > c) swap(&b, &c);
    if (a > b) swap(&a, &b);

    printf("%d %d %d\n", a, b, c);
    return 0;
}

How It Works

  1. The first swap gives 1, 3, 2.
  2. The second swap gives 1, 2, 3.
  3. The third comparison finds nothing to do. Every sort is built from compare-and-swap steps like these.

C Sorting Algorithms Topics

Every guide in this section with a one-line summary (12 tutorials).

Simple sorts

4 tutorials

Easy-to-write quadratic sorts.

TopicDescriptionTutorial
Sorting basicsLearn comparison functions, stability, and the standard qsort function.Try it
Bubble sortRepeatedly swap adjacent out-of-order items; trace each pass.Try it
Selection sortFind the smallest remaining element and place it next.Try it
Insertion sortGrow a sorted prefix by shifting larger items right.Try it

Efficient comparison sorts

4 tutorials

Divide-and-conquer and heap-based sorts.

TopicDescriptionTutorial
Merge sortSplit the array in half, sort each half, and merge them.Try it
Quick sortPartition by a pivot and recursively sort both sides.Try it
Heap sortBuild a max heap then repeatedly move the maximum to the end.Try it
Shell sortSort elements far apart first, then shrink the gap to 1.Try it

Non-comparison sorts

3 tutorials

Counting, bucket, and radix sort.

TopicDescriptionTutorial
Counting sortCount occurrences of small integer keys and rebuild the array.Try it
Bucket sortSpread values into range buckets, sort each bucket, and concatenate.Try it
Radix sortSort integers by each digit using stable counting sort passes.Try it

Comparison

1 tutorial

Choose the right sort.

TopicDescriptionTutorial
Sorting comparisonsCount comparisons and moves for three simple sorts on the same input.Try it

Edge Cases & Pitfalls

Common mistakes when learning sorting in C.

stable

Assuming stability

Quick sort and heap sort are not stable; equal keys may be reordered.

worst

Quick sort on sorted input

A naive last-element pivot degrades to O(n^2) on already sorted data.

n2

Using O(n^2) sorts on big data

Bubble sort on a million items takes about 10^12 steps. Use an O(n log n) sort.

Key Takeaways

  • Simple sorts: O(n^2).
  • Efficient sorts: O(n log n).
  • Non-comparison sorts need restricted keys.

One line: Sorting arranges items in order so that searching, merging, and grouping become faster.

Frequently Asked Questions

For general data, quick sort and merge sort (O(n log n)) are the standard fast choices; qsort() from <stdlib.h> is the usual library option.
A sort that keeps equal elements in their original relative order, such as merge sort and insertion sort.
Insertion sort is often fastest for very small or nearly sorted arrays.

Next: Sorting Basics in C

Learn comparison functions, stability, and the standard qsort function.

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