C Merge Sort

Intermediate
6 min read
Updated: Nov 2026
1 example

Divide, Sort, Merge

Merge sort splits the array in half, recursively sorts each half, and merges the two sorted halves into one. Merging is linear, and there are log n levels, so total time is O(n log n) in every case. It is stable but needs O(n) extra space for the merge buffer.

Remember
msort(lo, hi):
    if lo >= hi: return
    mid = (lo + hi) / 2
    msort(lo, mid); msort(mid + 1, hi)
    merge(lo, mid, hi)

Cheat Sheet

Keep these facts handy while you work with Merge Sort in C.

TopicDetail
TimeO(n log n) always
SpaceO(n) buffer
StableYes
Mergesn - 1 for n items

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Quick Sort.

Example 1 — Sort seven values and count merges

Seven leaves need six merges.

C
#include <stdio.h>

static int merges;

static void merge(int a[], int tmp[], int lo, int mid, int hi) {
    int i = lo, j = mid + 1, k = lo;
    while (i <= mid && j <= hi) tmp[k++] = (a[i] <= a[j]) ? a[i++] : a[j++];
    while (i <= mid) tmp[k++] = a[i++];
    while (j <= hi)  tmp[k++] = a[j++];
    for (k = lo; k <= hi; k++) a[k] = tmp[k];
    merges++;
}

static void msort(int a[], int tmp[], int lo, int hi) {
    if (lo >= hi) return;
    int mid = lo + (hi - lo) / 2;
    msort(a, tmp, lo, mid);
    msort(a, tmp, mid + 1, hi);
    merge(a, tmp, lo, mid, hi);
}

int main(void) {
    int a[] = {38, 27, 43, 3, 9, 82, 10}, tmp[7];
    msort(a, tmp, 0, 6);

    printf("sorted:");
    for (int i = 0; i < 7; i++) printf(" %d", a[i]);
    printf("\nmerges: %d\n", merges);
    return 0;
}

How It Works

  1. The recursion splits down to single elements.
  2. Each merge combines two sorted runs; <= keeps the sort stable.
  3. Seven single-element runs need six merges to become one.

Edge Cases & Pitfalls

Check these before relying on Merge Sort in real programs.

alloc

Allocating inside merge

Allocating a buffer per merge is slow. Allocate once and reuse.

mid

Overflow in (lo + hi) / 2

Use lo + (hi - lo) / 2 for very large arrays.

Key Takeaways

  • Divide and conquer.
  • O(n log n) always.
  • Stable with O(n) extra space.

One line: Merge sort splits the array in half, recursively sorts each half, and merges the two sorted halves into one.

Frequently Asked Questions

O(n log n) in the best, average, and worst cases.
The merge step writes into a temporary buffer so it does not overwrite unmerged elements.
Continue with "C Quick Sort" next. Partition by a pivot and recursively sort both sides. The C Sorting Algorithms hub lists every topic in order.

Next: Quick Sort

Partition by a pivot and recursively sort both sides.

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.

12 people found this page helpful