Merge sort is a divide-and-conquer sorting algorithm: split the array in half, sort each half recursively, then merge the two sorted halves into one. It runs in O(n log n) time in every case, is stable, and needs O(n) extra memory for the merge step. In C you implement it with a recursive function plus a temporary buffer that is allocated once.
C has no built-in generic sort that is guaranteed stable. qsort from the standard library makes no stability promise and its algorithm is implementation-defined, so a hand-written merge sort is useful when you need stability or predictable O(n log n) worst-case behaviour.
Sorting {38, 27, 43, 3} goes like this:
{38, 27} and {43, 3}.{38} and {27} into {27, 38}, {43} and {3} into {3, 43}, then merge those into {3, 27, 38, 43}.The merge step repeatedly compares the front elements of the two sorted halves and copies the smaller one to the output. When one half is empty, the rest of the other half is copied as is.
This version sorts an int array using half-open index ranges (lo inclusive, hi exclusive). It allocates the temporary buffer once in the public function, not in every recursive call, which avoids thousands of malloc calls.
Build and run it with gcc -std=c17 -Wall -Wextra -O2 msort.c -o msort && ./msort:
lo + (hi - lo) / 2 cannot overflow, unlike (lo + hi) / 2 for very large indexes.<= when comparing a[i] and a[j] takes the left element first on ties, so equal elements keep their original order.size_t is used for indexes because array lengths are size_t. A signed int index breaks for arrays larger than INT_MAX.malloc can fail, so merge_sort returns an error code instead of crashing.| Case | Time | Extra space |
|---|---|---|
| Best | O(n log n) | O(n) |
| Average | O(n log n) | O(n) |
| Worst | O(n log n) | O(n) |
The recursion depth is about log2(n), so stack usage is small even for millions of elements. The O(n) buffer is the main cost compared with in-place algorithms such as heapsort.
To sort anything other than int, follow the qsort pattern: take an element size and a comparison function pointer, and copy elements with memcpy.
The merge step then compares (char *)base + i * size with (char *)base + j * size through cmp, and copies size bytes at a time. The comparison function has the same contract as for qsort: negative, zero or positive.
qsort when you want the shortest code and do not need stability.Yes, if the merge takes from the left half when two elements are equal, which is what <= does in the example above.
Merging two sorted halves in place is complicated and slow, so the usual version writes into a temporary buffer of the same size as the array and copies the result back.
Not usually on arrays. Quicksort is often faster in practice because of better cache behaviour and no extra buffer, but merge sort has a guaranteed O(n log n) worst case and is stable.
Yes. A bottom-up version merges runs of width 1, 2, 4 and so on in a loop, which removes recursion entirely and uses the same merge function.
Merge sort splits an array, sorts the halves recursively and merges them. The C implementation above allocates a single temporary buffer, uses half-open ranges, stays stable and reports allocation failure. It guarantees O(n log n) time at the cost of O(n) extra memory.