3-way Merge Sort in C++



Merge sort is a popular sorting algorithm that follows the divide-and-conquer strategy. It works by recursively dividing the input array into two halves, sorting each half, and then merging them. With a time complexity of O(n log n).

3-Way Merge Sort

An optimized variation of merge sort is the 3-way merge sort, where the array is divided into three equal parts instead of two. This reduces the number of recursive calls and improves performance in certain scenarios.

3-Way Merge Sort algorithm

Following are the steps (algorithm) to implement the 3-way merge sort:

  • Divide the array into three equal parts using two midpoints.
  • Recursively sort each of the three parts.
  • Create a temporary array to hold the merged result.
  • Use three pointers to track the current index in each of the three subarrays.
  • At each step, select the smallest of the three elements and add it to the temporary array.
  • Once all elements are merged, copy the temporary array back into the original array.
  • Repeat the process until the entire array is sorted.

Working of 3-Way Merge Sort with Examples

Let's see some example scenarios to understand 3-Way Merge Sort better:

Scenario 1

Input: arr = [6, 3, 9, 5, 2, 8]

Step 1: Divide into three parts:

  • [6, 3], [9, 5], [2, 8]

Step 2: Recursively sort each part:

  • [6, 3] → [3, 6]
  • [9, 5] → [5, 9]
  • [2, 8] → [2, 8] (already sorted)

Step 3: Merge [3, 6], [5, 9], and [2, 8]

  • Compare 3, 5, and 2 → choose 2
  • Compare 3, 5, and 8 → choose 3
  • Compare 6, 5, and 8 → choose 5
  • Compare 6, 9, and 8 → choose 6
  • Compare 9 and 8 → choose 8
  • Remaining → 9

Output: [2, 3, 5, 6, 8, 9]

Explanation: The array is sorted in ascending order.

Scenario 2

Input: arr = [24, -18]

Step 1: Divide as:

  • [-18], [24], []

Step 2: Subarrays with one element are already sorted.

Step 3: Merge [-18], [24], and []

  • Merge directly → [-18, 24]

Output: [-18, 24]

Explanation: The array is sorted with the smallest number first.

Implementation of 3-Way Merge Sort

Here is the implementation of 3-way merge sort using C++ program:

#include <bits/stdc++.h>
using namespace std;
void merge(int arr[], int left, int mid1, int mid2, int right) {
   // Sizes of three subarrays
   int size1 = mid1 - left + 1;
   int size2 = mid2 - mid1;
   int size3 = right - mid2;
   // Temporary arrays for three parts
   vector < int > leftArr(size1), midArr(size2), rightArr(size3);
   // Copy data to temporary arrays
   for (int i = 0; i < size1; i++) {
      leftArr[i] = arr[left + i];
   }
   for (int i = 0; i < size2; i++) {
      midArr[i] = arr[mid1 + 1 + i];
   }
   for (int i = 0; i < size3; i++) {
      rightArr[i] = arr[mid2 + 1 + i];
   }
   // Merge three sorted subarrays
   int i = 0, j = 0, k = 0, index = left;
   while (i < size1 || j < size2 || k < size3) {
      int minValue = INT_MAX, minIdx = -1;
      // Find the smallest among the three current elements
      if (i < size1 && leftArr[i] < minValue) {
         minValue = leftArr[i];
         minIdx = 0;
      }
      if (j < size2 && midArr[j] < minValue) {
         minValue = midArr[j];
         minIdx = 1;
      }
      if (k < size3 && rightArr[k] < minValue) {
         minValue = rightArr[k];
         minIdx = 2;
      }

      // Place the smallest element in the merged array
      if (minIdx == 0) {
         arr[index++] = leftArr[i++];
      } else if (minIdx == 1) {
         arr[index++] = midArr[j++];
      } else {
         arr[index++] = rightArr[k++];
      }
   }
}
void three_way_merge_sort(int arr[], int left, int right) {
   // Base case: If single element, return
   if (left >= right) {
      return;
   }
   // Finding two midpoints for 3-way split
   int mid1 = left + (right - left) / 3;
   int mid2 = left + 2 * (right - left) / 3;

   // Recursively sort first third
   three_way_merge_sort(arr, left, mid1);

   // Recursively sort second third
   three_way_merge_sort(arr, mid1 + 1, mid2);

   // Recursively sort last third
   three_way_merge_sort(arr, mid2 + 1, right);
   // Merge the sorted parts
   merge(arr, left, mid1, mid2, right);
}

int main() {
   int arr[] = {6, 3, 9, 5, 2, 8};
   int n = sizeof(arr) / sizeof(int);

   three_way_merge_sort(arr, 0, n - 1);

   for (int i = 0; i < n; i++) {
      cout << arr[i] << " ";
   }
   return 0;
}

Following is the output:

2 3 5 6 8 9

Conclusion

In this article, we learned how 3-way merge sort works. Unlike traditional merge sort, it divides the array into three parts and sorts them recursively before merging.

Updated on: 2025-07-29T16:22:31+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements