Merge Sort in C++
Sort an array with merge sort in C++: split it in half recursively, sort each half, and merge. Explains the recursion tree, the merge step, and O(n log n).
1#include <iostream>2using namespace std;34void merge(int arr[], int left, int mid, int right) {5 int n1 = mid - left + 1;6 int n2 = right - mid;78 // Create temporary arrays9 int L[n1], R[n2];1011 // Copy data to temp arrays12 for (int i = 0; i < n1; i++)13 L[i] = arr[left + i];14 for (int j = 0; j < n2; j++)15 R[j] = arr[mid + 1 + j];1617 // Merge temp arrays back18 int i = 0, j = 0, k = left;19 while (i < n1 && j < n2) {20 if (L[i] <= R[j]) {21 arr[k] = L[i];22 i++;23 } else {24 arr[k] = R[j];25 j++;26 }27 k++;28 }2930 // Copy remaining elements31 while (i < n1) {32 arr[k] = L[i];33 i++;34 k++;35 }36 while (j < n2) {37 arr[k] = R[j];38 j++;39 k++;40 }41}4243void mergeSort(int arr[], int left, int right) {44 if (left < right) {45 int mid = left + (right - left) / 2;4647 // Sort first and second halves48 mergeSort(arr, left, mid);49 mergeSort(arr, mid + 1, right);5051 // Merge sorted halves52 merge(arr, left, mid, right);53 }54}5556void printArray(int arr[], int n) {57 for (int i = 0; i < n; i++) {58 cout << arr[i] << " ";59 }60 cout << endl;61}6263int main() {64 int arr[] = {64, 34, 25, 12, 22, 11, 90};65 int n = sizeof(arr) / sizeof(arr[0]);6667 cout << "Original array: ";68 printArray(arr, n);6970 mergeSort(arr, 0, n - 1);7172 cout << "Sorted array: ";73 printArray(arr, n);7475 return 0;76}
Sample Output
Sorted array: 11 12 22 25 34 64 90
After all merges.
In this one
- How divide and conquer splits the problem
- How the base case left < right stops recursion
- How merge combines two sorted halves
- Why mid = left + (right - left) / 2 avoids overflow
How it works
- 1
mergeSort(0, 6) splits at mid = 3.
- 2
Each half is split again until single elements remain.
- 3
merge copies halves into L and R and merges them back with <=.
- 4
Results bubble up to a fully sorted array.
Edge cases
Large arrays
Sort a million ints.
L[n1] and R[n2] are run-time sized arrays on the stack, an extension that can overflow the stack for large n. Use vector<int>.
mid = (left + right) / 2
Use very large indexes.
left + right can overflow int. left + (right - left) / 2 cannot.
What's going on
The program sorts an array of integers using the Merge Sort algorithm. For example:
Merge Sort uses a divide-and-conquer approach: it divides the problem into smaller subproblems, solves them recursively, and combines the solutions.
Header File Used
#include <iostream>
This header provides:
Understanding Merge Sort
Divide-and-Conquer Strategy:
Visual Example:
[64, 34, 25, 12, 22, 11, 90]
↓ Divide
[64, 34, 25] [12, 22, 11, 90]
↓ Divide ↓ Divide
[64] [34, 25] [12, 22] [11, 90]
↓ Merge ↓ Merge ↓ Merge
[64] [25, 34] [12, 22] [11, 90]
↓ Merge ↓ Merge
[25, 34, 64] [11, 12, 22, 90]
↓ Merge
[11, 12, 22, 25, 34, 64, 90]
Function: merge()
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];
// Copy data to temp arrays
for (int i = 0; i < n1; i++)
L[i] = arr[left + i];
for (int j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
// Merge temp arrays back
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
// Copy remaining elements
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}
How it works:
Function: mergeSort()
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
How it works:
Understanding the Merge Process
Merging Two Sorted Arrays:
Left: [25, 34, 64] Right: [11, 12, 22, 90]
Step 1: Compare 25 and 11 → 11 is smaller → place 11
Result: [11, ...]
Step 2: Compare 25 and 12 → 12 is smaller → place 12
Result: [11, 12, ...]
Step 3: Compare 25 and 22 → 22 is smaller → place 22
Result: [11, 12, 22, ...]
Step 4: Compare 25 and 90 → 25 is smaller → place 25
Result: [11, 12, 22, 25, ...]
Step 5: Compare 34 and 90 → 34 is smaller → place 34
Result: [11, 12, 22, 25, 34, ...]
Step 6: Compare 64 and 90 → 64 is smaller → place 64
Result: [11, 12, 22, 25, 34, 64, ...]
Step 7: Right array exhausted, copy remaining from left
Result: [11, 12, 22, 25, 34, 64, 90]
Time and Space Complexity
Time Complexity: O(n log n) in all cases
Space Complexity: O(n)
Stability: Stable
When to Use Merge Sort
Best For:
Not Recommended For:
Important Considerations
Recursive Nature:
Temporary Arrays:
Mid Calculation:
return 0;
This ends the program successfully.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Time complexity in all cases?
Pick an answer to check it.
Question 2
What stops the recursion?
Pick an answer to check it.
Questions people ask
Is merge sort stable?
Yes, because the merge uses <=, taking from the left half on ties.
Extra memory?
O(n) for the temporary arrays.
What does std::stable_sort use?
Usually a merge sort variant.