Sorting Lesson Progress
0%
C Programming · Sorting Algorithms

Sorting Algorithms in C

Learn the most common sorting methods, how they work, and when to use them — with simple examples in C.

0–10 min · Basics
10–25 min · Bubble & Selection
25–40 min · Insertion & Merge
40–60 min · Quick Sort, Practice
1

What is Sorting?

0 – 10 min

A sorting algorithm arranges data in a specific order, usually ascending or descending. In C, sorting is often used with arrays.

  • Ascending: 1, 2, 3, 4, 5
  • Descending: 5, 4, 3, 2, 1
  • Why it matters: sorting makes searching, comparing, and displaying data easier
💡 In C, we usually sort arrays by swapping elements using a temporary variable.
2

Bubble Sort

10 – 18 min

Bubble sort compares adjacent elements and swaps them if they are in the wrong order. It is simple, but slow for large arrays.

bubble_sort.c
C
#include <stdio.h>

int main() {
    int arr[] = {5, 1, 4, 2, 8};
    int n = sizeof(arr) / sizeof(arr[0]);
    int i, j, temp;

    for (i = 0; i < n - 1; i++) {
        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;
            }
        }
    }

    printf("Sorted array: ");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}
PropertyValue
Best forVery small arrays
Time complexityO(n²)
Space complexityO(1)
3

Selection Sort

18 – 25 min

Selection sort finds the smallest element in the unsorted part and places it at the beginning.

selection_sort.c
C
#include <stdio.h>

int main() {
    int arr[] = {29, 10, 14, 37, 13};
    int n = sizeof(arr) / sizeof(arr[0]);
    int i, j, minIndex, temp;

    for (i = 0; i < n - 1; i++) {
        minIndex = i;
        for (j = i + 1; j < n; j++) {
            if (arr[j] < arr[minIndex]) {
                minIndex = j;
            }
        }
        temp = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = temp;
    }

    printf("Sorted array: ");
    for (i = 0; i < n; i++) printf("%d ", arr[i]);
    printf("\n");
    return 0;
}
  • Find the smallest value
  • Swap it with the current position
  • Repeat until the array is sorted
4

Insertion Sort

25 – 33 min

Insertion sort builds the sorted part one element at a time, like sorting cards in your hand.

insertion_sort.c
C
#include <stdio.h>

int main() {
    int arr[] = {12, 11, 13, 5, 6};
    int n = sizeof(arr) / sizeof(arr[0]);
    int i, key, j;

    for (i = 1; i < n; i++) {
        key = arr[i];
        j = i - 1;
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = key;
    }

    printf("Sorted array: ");
    for (i = 0; i < n; i++) printf("%d ", arr[i]);
    printf("\n");
    return 0;
}
💡 Insertion sort works well when the array is already mostly sorted.
5

Merge Sort

33 – 45 min

Merge sort uses a divide-and-conquer strategy: split the array into halves, sort each half, then merge them back together.

PropertyValue
Time complexityO(n log n)
Space complexityO(n)
Best forLarge data sets
merge_sort.c
C
#include <stdio.h>

void merge(int arr[], int l, int m, int r) {
    int i, j, k;
    int n1 = m - l + 1;
    int n2 = r - m;
    int L[20], R[20];

    for (i = 0; i < n1; i++) L[i] = arr[l + i];
    for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j];

    i = 0; j = 0; k = l;
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) arr[k++] = L[i++];
        else arr[k++] = R[j++];
    }
    while (i < n1) arr[k++] = L[i++];
    while (j < n2) arr[k++] = R[j++];
}

void mergeSort(int arr[], int l, int r) {
    if (l < r) {
        int m = l + (r - l) / 2;
        mergeSort(arr, l, m);
        mergeSort(arr, m + 1, r);
        merge(arr, l, m, r);
    }
}
6

Quick Sort

45 – 55 min

Quick sort chooses a pivot element, partitions the array around it, and recursively sorts the two parts. It is usually very fast in practice.

quick_sort.c
C
#include <stdio.h>

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

int partition(int arr[], int low, int high) {
    int pivot = arr[high];
    int i = low - 1;
    for (int j = low; j < high; j++) {
        if (arr[j] < pivot) {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return i + 1;
}

void quickSort(int arr[], int low, int high) {
    if (low < high) {
        int pi = partition(arr, low, high);
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}
⚠️ Note: Quick sort is fast on average, but its performance can drop if the pivot choice is bad.
comparison
AlgorithmBest CaseAverageWorstExtra Space
Bubble SortO(n)O(n²)O(n²)O(1)
Selection SortO(n²)O(n²)O(n²)O(1)
Insertion SortO(n)O(n²)O(n²)O(1)
Merge SortO(n log n)O(n log n)O(n log n)O(n)
Quick SortO(n log n)O(n log n)O(n²)O(log n)
Q

Quick Quiz

Question 1 of 4

Which sorting algorithm compares adjacent elements?

Question 2 of 4

Which algorithm is usually best for nearly sorted arrays?

Question 3 of 4

Which sorting algorithm uses divide and conquer?

Question 4 of 4

Which sorting algorithm typically uses a pivot?

Lesson Checklist

  • I know what sorting means
  • I can explain bubble sort
  • I can explain selection sort
  • I can explain insertion sort
  • I know why merge sort is efficient
  • I know quick sort uses a pivot
  • I understand sorting complexity basics
  • I completed the quiz

Next Practice Idea

Try this next
  • Sort an array of student marks using ascending order practice
  • Modify the code to sort in descending order practice
  • Compare time complexity of different algorithms theory