Merge Sort is a Divide and Conquer sorting algorithm that divides an array into two halves, recursively sorts each half, and then merges the two sorted halves. This algorithm is efficient and works well for large datasets. It has a time complexity of O(n log n) in all cases (best, worst, and average), which makes it one of the more efficient sorting algorithms for large datasets.
The Merge Sort algorithm follows three main steps:
The first step in Merge Sort is to divide the unsorted array into two halves. If the array contains only one element, it is already sorted (since a single element is trivially sorted).
The algorithm then recursively sorts the two halves by applying the same strategy: divide the halves further until each subarray has only one element.
Once we have the smallest subarrays (containing one element each), the merge step begins. The merge step takes two sorted subarrays and combines them into a single sorted array.
During the merge process, we compare the smallest (leftmost) elements of the two subarrays, insert the smaller element into the result array, and move the pointer for that subarray. This process continues until all elements from both subarrays are merged into the final sorted array.
Here is the pseudocode for the Merge Sort algorithm:
// Merge function to merge two sorted halves
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1; // Length of the left subarray
int n2 = right - mid; // Length of the right subarray
// Temporary arrays to store the two halves
int leftArr[n1], rightArr[n2];
// Copy data to temporary arrays leftArr[] and rightArr[]
for (int i = 0; i < n1; i++) {
leftArr[i] = arr[left + i];
}
for (int i = 0; i < n2; i++) {
rightArr[i] = arr[mid + 1 + i];
}
// Merge the temporary arrays back into arr[left...right]
int i = 0; // Initial index of leftArr[]
int j = 0; // Initial index of rightArr[]
int k = left; // Initial index of merged subarray
while (i < n1 && j < n2) {
if (leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
}
k++;
}
// Copy the remaining elements of leftArr[], if any
while (i < n1) {
arr[k] = leftArr[i];
i++;
k++;
}
// Copy the remaining elements of rightArr[], if any
while (j < n2) {
arr[k] = rightArr[j];
j++;
k++;
}
}
// MergeSort function to recursively divide and sort the array
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2; // Find the middle point
// Recursively sort the first and second halves
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
// Merge the sorted halves
merge(arr, left, mid, right);
}
}
Let’s walk through an example using the array:
[38, 27, 43, 3, 9, 82, 10]
[38, 27, 43][3, 9, 82, 10]Split [38, 27, 43] into [38] and [27, 43].
[38] is already sorted (only one element).[27, 43]:
[27, 43] into [27] and [43] (both are sorted).[27] and [43] into [27, 43].Now merge [38] and [27, 43]:
38 and 27. Since 27 is smaller, add 27 to the sorted array.38 and 43. Since 38 is smaller, add 38 to the sorted array.43 to the sorted array.Sorted left half: [27, 38, 43].
Split [3, 9, 82, 10] into [3, 9] and [82, 10].
[3, 9]:
[3, 9] into [3] and [9] (both are sorted).[3] and [9] into [3, 9].[82, 10]:
[82, 10] into [82] and [10] (both are sorted).[82] and [10] into [10, 82].Now merge [3, 9] and [10, 82]:
3 and 10. Since 3 is smaller, add 3 to the sorted array.9 and 10. Since 9 is smaller, add 9 to the sorted array.10 and 82 to the sorted array.Sorted right half: [3, 9, 10, 82].
Now merge the two sorted halves: [27, 38, 43] and [3, 9, 10, 82].
27 and 3. Since 3 is smaller, add 3 to the sorted array.27 and 9. Since 9 is smaller, add 9 to the sorted array.27 and 10. Since 10 is smaller, add 10 to the sorted array.27 and 82. Since 27 is smaller, add 27 to the sorted array.38 and 82. Since 38 is smaller, add 38 to the sorted array.43 and 82. Since 43 is smaller, add 43 to the sorted array.82 to the sorted array.Final sorted array: [3, 9, 10, 27, 38, 43, 82].
Open this section to load past papers