Open In App

3-way Merge Sort

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Merge sort involves recursively splitting the array into 2 parts, sorting and finally merging them. A variant of merge sort is called 3-way merge sort where instead of splitting the array into 2 parts we split it into 3 parts

Merge sort recursively breaks down the arrays to subarrays of size half. Similarly, 3-way Merge sort breaks down the arrays to subarrays of size one third. 
 

Examples:  

Input: arr = [45, -2, -45, 78, 30, -42, 10, 19, 73, 93] 
Output: [-45, -42, -2, 10, 19, 30, 45, 73, 78, 93]

Input: arr = [23, -19]
Output: [-19, 23]

 

C++




// C++ Program to perform 3 way Merge Sort
 
#include <bits/stdc++.h>
using namespace std;
 
/* Merge the sorted ranges [low, mid1), [mid1,mid2)
and [mid2, high) mid1 is first midpoint
index in overall range to merge mid2 is second
midpoint index in overall range to merge*/
void merge(vector<int> &gArray, int low, int mid1,
           int mid2, int high, vector<int> &destArray)
{
    int i = low, j = mid1, k = mid2, l = low;
 
    // Choose smaller of the smallest in the three ranges
    while ((i < mid1) && (j < mid2) && (k < high))
    {
        if(gArray[i] < gArray[j])
        {
            if(gArray[i] < gArray[k])
            {
                destArray[l++] = gArray[i++];
            }
            else
            {
                destArray[l++] = gArray[k++];
            }
        }
        else
        {
            if(gArray[j] < gArray[k])
            {
                destArray[l++] = gArray[j++];
            }
            else
            {
                destArray[l++] = gArray[k++];
            }
        }
    }
 
    // Case where first and second ranges
    // have remaining values
    while ((i < mid1) && (j < mid2))
    {
        if(gArray[i] < gArray[j])
        {
            destArray[l++] = gArray[i++];
        }
        else
        {
            destArray[l++] = gArray[j++];
        }
    }
 
    // case where second and third ranges
    // have remaining values
    while ((j < mid2) && (k < high))
    {
        if(gArray[j] < gArray[k])
        {
            destArray[l++] = gArray[j++];
        }
        else
        {
            destArray[l++] = gArray[k++];
        }
    }
 
    // Case where first and third ranges have
    // remaining values
    while ((i < mid1) && (k < high))
    {
        if(gArray[i] < gArray[k])
        {
            destArray[l++] = gArray[i++];
        }
        else
        {
            destArray[l++] = gArray[k++];
        }
    }
 
    // Copy remaining values from the first range
    while (i < mid1)
        destArray[l++] = gArray[i++];
 
    // Copy remaining values from the second range
    while (j < mid2)
        destArray[l++] = gArray[j++];
 
    // Copy remaining values from the third range
    while (k < high)
        destArray[l++] = gArray[k++];
}
 
 
/* Performing the merge sort algorithm on the
given array of values in the rangeof indices
[low, high). low is minimum index, high is
maximum index (exclusive) */
void mergeSort3WayRec(vector<int> &gArray, int low,
                      int high, vector<int> &destArray)
{
    // If array size is 1 then do nothing
    if (high - low < 2)
        return;
 
    // Splitting array into 3 parts
    int mid1 = low + ((high - low) / 3);
    int mid2 = low + 2 * ((high - low) / 3) + 1;
 
    // Sorting 3 arrays recursively
    mergeSort3WayRec(destArray, low, mid1, gArray);
    mergeSort3WayRec(destArray, mid1, mid2, gArray);
    mergeSort3WayRec(destArray, mid2, high, gArray);
 
    // Merging the sorted arrays
    merge(destArray, low, mid1, mid2, high, gArray);
}
 
void mergeSort3Way(vector<int> &gArray, int n)
{
    // if array size is zero return null
    if (n == 0)
        return;
 
    // creating duplicate of given array
    vector<int> fArray(n);
 
    // copying elements of given array into
    // duplicate array
    for (int i = 0; i < n; i++)
        fArray[i] = gArray[i];
 
    // sort function
    mergeSort3WayRec(fArray, 0, n, gArray);
 
    // copy back elements of duplicate array
    // to given array
    for (int i = 0; i < n; i++)
        gArray[i] = fArray[i];
}
 
// Driver Code
int main()
{
    vector<int> data = {45, -2, -45, 78, 30,
                 -42, 10, 19, 73, 93};
    mergeSort3Way(data,10);
    cout << "After 3 way merge sort: ";
    for (int i = 0; i < 10; i++)
    {
        cout << data[i] << " ";
    }
    return 0;
}
 
// This code is contributed by Rashmi Kumari


Java




// Java program to perform 3 way Merge Sort
import java.util.*;
public class MergeSort3Way
{
    // Function  for 3-way merge sort process
    public static void mergeSort3Way(Integer[] gArray)
    {
        // if array of size is zero returns null
        if (gArray == null)
            return;
 
        // creating duplicate of given array
        Integer[] fArray = new Integer[gArray.length];
 
        // copying elements of given array into
        // duplicate array
        for (int i = 0; i < fArray.length; i++)
            fArray[i] = gArray[i];
 
        // sort function
        mergeSort3WayRec(fArray, 0, gArray.length, gArray);
 
        // copy back elements of duplicate array
        // to given array
        for (int i = 0; i < fArray.length; i++)
            gArray[i] = fArray[i];
    }
 
    /* Performing the merge sort algorithm on the
       given array of values in the rangeof indices
       [low, high).  low is minimum index, high is
       maximum index (exclusive) */
    public static void mergeSort3WayRec(Integer[] gArray,
                  int low, int high, Integer[] destArray)
    {
        // If array size is 1 then do nothing
        if (high - low < 2)
            return;
 
        // Splitting array into 3 parts
        int mid1 = low + ((high - low) / 3);
        int mid2 = low + 2 * ((high - low) / 3) + 1;
 
        // Sorting 3 arrays recursively
        mergeSort3WayRec(destArray, low, mid1, gArray);
        mergeSort3WayRec(destArray, mid1, mid2, gArray);
        mergeSort3WayRec(destArray, mid2, high, gArray);
 
        // Merging the sorted arrays
        merge(destArray, low, mid1, mid2, high, gArray);
    }
 
    /* Merge the sorted ranges [low, mid1), [mid1,
       mid2) and [mid2, high) mid1 is first midpoint
       index in overall range to merge mid2 is second
       midpoint index in overall range to merge*/
    public static void merge(Integer[] gArray, int low,
                           int mid1, int mid2, int high,
                                   Integer[] destArray)
    {
        int i = low, j = mid1, k = mid2, l = low;
 
        // choose smaller of the smallest in the three ranges
        while ((i < mid1) && (j < mid2) && (k < high))
        {
            if (gArray[i].compareTo(gArray[j]) < 0)
            {
                if (gArray[i].compareTo(gArray[k]) < 0)
                    destArray[l++] = gArray[i++];
 
                else
                    destArray[l++] = gArray[k++];
            }
            else
            {
                if (gArray[j].compareTo(gArray[k]) < 0)
                    destArray[l++] = gArray[j++];
                else
                    destArray[l++] = gArray[k++];
            }
        }
 
        // case where first and second ranges have
        // remaining values
        while ((i < mid1) && (j < mid2))
        {
            if (gArray[i].compareTo(gArray[j]) < 0)
                destArray[l++] = gArray[i++];
            else
                destArray[l++] = gArray[j++];
        }
 
        // case where second and third ranges have
        // remaining values
        while ((j < mid2) && (k < high))
        {
            if (gArray[j].compareTo(gArray[k]) < 0)
                destArray[l++] = gArray[j++];
 
            else
                destArray[l++] = gArray[k++];
        }
 
        // case where first and third ranges have
        // remaining values
        while ((i < mid1) && (k < high))
        {
            if (gArray[i].compareTo(gArray[k]) < 0)
                destArray[l++] = gArray[i++];
            else
                destArray[l++] = gArray[k++];
        }
 
        // copy remaining values from the first range
        while (i < mid1)
            destArray[l++] = gArray[i++];
 
        // copy remaining values from the second range
        while (j < mid2)
            destArray[l++] = gArray[j++];
 
        // copy remaining values from the third range
        while (k < high)
            destArray[l++] = gArray[k++];
    }
 
    // Driver function
    public static void main(String args[])
    {
        // test case of values
        Integer[] data = new Integer[] {45, -2, -45, 78,
                               30, -42, 10, 19, 73, 93};
        mergeSort3Way(data);
 
        System.out.println("After 3 way merge sort: ");
        for (int i = 0; i < data.length; i++)
            System.out.print(data[i] + " ");
    }
}


C#




// C# program to perform 3 way Merge Sort
using System;
public class MergeSort3Way
{
  // Function  for 3-way merge sort process
  public static void mergeSort3Way(int[] gArray)
  {
    // if array of size is zero returns null
    if (gArray == null)
      return;
 
    // creating duplicate of given array
    int[] fArray = new int[gArray.Length];
 
    // copying elements of given array into
    // duplicate array
    for (int i = 0; i < fArray.Length; i++)
      fArray[i] = gArray[i];
 
    // sort function
    mergeSort3WayRec(fArray, 0, gArray.Length, gArray);
 
    // copy back elements of duplicate array
    // to given array
    for (int i = 0; i < fArray.Length; i++)
      gArray[i] = fArray[i];
  }
 
  /* Performing the merge sort algorithm on the
       given array of values in the rangeof indices
       [low, high).  low is minimum index, high is
       maximum index (exclusive) */
  public static void mergeSort3WayRec(int[] gArray,
                                      int low, int high, int[] destArray)
  {
    // If array size is 1 then do nothing
    if (high - low < 2)
      return;
 
    // Splitting array into 3 parts
    int mid1 = low + ((high - low) / 3);
    int mid2 = low + 2 * ((high - low) / 3) + 1;
 
    // Sorting 3 arrays recursively
    mergeSort3WayRec(destArray, low, mid1, gArray);
    mergeSort3WayRec(destArray, mid1, mid2, gArray);
    mergeSort3WayRec(destArray, mid2, high, gArray);
 
    // Merging the sorted arrays
    merge(destArray, low, mid1, mid2, high, gArray);
  }
 
  /* Merge the sorted ranges [low, mid1), [mid1,
       mid2) and [mid2, high) mid1 is first midpoint
       index in overall range to merge mid2 is second
       midpoint index in overall range to merge*/
  public static void merge(int[] gArray, int low,
                           int mid1, int mid2, int high,
                           int[] destArray)
  {
    int i = low, j = mid1, k = mid2, l = low;
 
    // choose smaller of the smallest in the three ranges
    while ((i < mid1) && (j < mid2) && (k < high))
    {
      if (gArray[i].CompareTo(gArray[j]) < 0)
      {
        if (gArray[i].CompareTo(gArray[k]) < 0)
          destArray[l++] = gArray[i++];
 
        else
          destArray[l++] = gArray[k++];
      }
      else
      {
        if (gArray[j].CompareTo(gArray[k]) < 0)
          destArray[l++] = gArray[j++];
        else
          destArray[l++] = gArray[k++];
      }
    }
 
    // case where first and second ranges have
    // remaining values
    while ((i < mid1) && (j < mid2))
    {
      if (gArray[i].CompareTo(gArray[j]) < 0)
        destArray[l++] = gArray[i++];
      else
        destArray[l++] = gArray[j++];
    }
 
    // case where second and third ranges have
    // remaining values
    while ((j < mid2) && (k < high))
    {
      if (gArray[j].CompareTo(gArray[k]) < 0)
        destArray[l++] = gArray[j++];
 
      else
        destArray[l++] = gArray[k++];
    }
 
    // case where first and third ranges have
    // remaining values
    while ((i < mid1) && (k < high))
    {
      if (gArray[i].CompareTo(gArray[k]) < 0)
        destArray[l++] = gArray[i++];
      else
        destArray[l++] = gArray[k++];
    }
 
    // copy remaining values from the first range
    while (i < mid1)
      destArray[l++] = gArray[i++];
 
    // copy remaining values from the second range
    while (j < mid2)
      destArray[l++] = gArray[j++];
 
    // copy remaining values from the third range
    while (k < high)
      destArray[l++] = gArray[k++];
  }
 
  // Driver function
  public static void Main()
  {
    // test case of values
    int[] data = new int[] {45, -2, -45, 78,
                            30, -42, 10, 19, 73, 93};
    mergeSort3Way(data);
 
    Console.Write("After 3 way merge sort: ");
    for (int i = 0; i < data.Length; i++)
      Console.Write(data[i] + " ");
  }
}
 
// This code is contributed by saurabh_jaiswal.


Javascript




<script>
 
// JavaScript Program to perform 3 way Merge Sort
 
/* Merge the sorted ranges [low, mid1), [mid1,mid2)
and [mid2, high) mid1 is first midpoint
index in overall range to merge mid2 is second
midpoint index in overall range to merge*/
function merge(gArray, low, mid1,mid2, high, destArray)
{
    let i = low, j = mid1, k = mid2, l = low;
 
    // choose smaller of the smallest in the three ranges
    while ((i < mid1) && (j < mid2) && (k < high))
    {
        if(gArray[i] < gArray[j])
        {
            if(gArray[i] < gArray[k])
            {
                destArray[l++] = gArray[i++];
            }
            else
            {
                destArray[l++] = gArray[k++];
            }
        }
        else
        {
            if(gArray[j] < gArray[k])
            {
                destArray[l++] = gArray[j++];
            }
            else
            {
                destArray[l++] = gArray[k++];
            }
        }
    }
 
    // case where first and second ranges
    // have remaining values
    while ((i < mid1) && (j < mid2))
    {
        if(gArray[i] < gArray[j])
        {
            destArray[l++] = gArray[i++];
        }
        else
        {
            destArray[l++] = gArray[j++];
        }
    }
 
    // case where second and third ranges
    // have remaining values
    while ((j < mid2) && (k < high))
    {
        if(gArray[j] < gArray[k])
        {
            destArray[l++] = gArray[j++];
        }
        else
        {
            destArray[l++] = gArray[k++];
        }
    }
 
    // case where first and third ranges have
    // remaining values
    while ((i < mid1) && (k < high))
    {
        if(gArray[i] < gArray[k])
        {
            destArray[l++] = gArray[i++];
        }
        else
        {
            destArray[l++] = gArray[k++];
        }
    }
 
    // copy remaining values from the first range
    while (i < mid1)
        destArray[l++] = gArray[i++];
 
    // copy remaining values from the second range
    while (j < mid2)
        destArray[l++] = gArray[j++];
 
    // copy remaining values from the third range
    while (k < high)
        destArray[l++] = gArray[k++];
}
 
 
/* Performing the merge sort algorithm on the
given array of values in the rangeof indices
[low, high). low is minimum index, high is
maximum index (exclusive) */
function mergeSort3WayRec(gArray, low,high, destArray)
{
    // If array size is 1 then do nothing
    if (high - low < 2)
        return;
 
    // Splitting array into 3 parts
    let mid1 = low + Math.floor((high - low) / 3);
    let mid2 = low + 2 * Math.floor((high - low) / 3) + 1;
 
    // Sorting 3 arrays recursively
    mergeSort3WayRec(destArray, low, mid1, gArray);
    mergeSort3WayRec(destArray, mid1, mid2, gArray);
    mergeSort3WayRec(destArray, mid2, high, gArray);
 
    // Merging the sorted arrays
    merge(destArray, low, mid1, mid2, high, gArray);
}
 
function mergeSort3Way(gArray,n)
{
    // if array size is zero return null
    if (n == 0)
        return;
 
    // creating duplicate of given array
    let fArray = new Array(n);
 
    // copying elements of given array into
    // duplicate array
    for (let i = 0; i < n; i++)
        fArray[i] = gArray[i];
 
    // sort function
    mergeSort3WayRec(fArray, 0, n, gArray);
 
    // copy back elements of duplicate array
    // to given array
    for (let i = 0; i < n; i++)
        gArray[i] = fArray[i];
}
 
// Driver Code
 
let data = [45, -2, -45, 78, 30,
                -42, 10, 19, 73, 93];
mergeSort3Way(data,10);
document.write("After 3 way merge sort: ","</br>");
for (let i = 0; i < 10; i++)
{
    document.write(data[i]," ");
}
 
// This code is contributed by shinjanpatra
 
</script>


Python3




# Python Program to perform 3 way Merge Sort
 
""" Merge the sorted ranges [low, mid1), [mid1,mid2)
and [mid2, high) mid1 is first midpoint
index in overall range to merge mid2 is second
midpoint index in overall range to merge"""
 
 
def merge(gArray, low, mid1, mid2, high, destArray):
    i = low
    j = mid1
    k = mid2
    l = low
 
    # Choose smaller of the smallest in the three ranges
    while ((i < mid1) and (j < mid2) and (k < high)):
        if(gArray[i] < gArray[j]):
            if(gArray[i] < gArray[k]):
                destArray[l] = gArray[i]
                l += 1
                i += 1
            else:
                destArray[l] = gArray[k]
                l += 1
                k += 1
        else:
            if(gArray[j] < gArray[k]):
                destArray[l] = gArray[j]
                l += 1
                j += 1
            else:
                destArray[l] = gArray[k]
                l += 1
                k += 1
 
    # Case where first and second ranges
    # have remaining values
    while ((i < mid1) and (j < mid2)):
        if(gArray[i] < gArray[j]):
            destArray[l] = gArray[i]
            l += 1
            i += 1
        else:
            destArray[l] = gArray[j]
            l += 1
            j += 1
 
    # case where second and third ranges
    # have remaining values
    while ((j < mid2) and (k < high)):
        if(gArray[j] < gArray[k]):
            destArray[l] = gArray[j]
            l += 1
            j += 1
        else:
            destArray[l] = gArray[k]
            l += 1
            k += 1
 
    # Case where first and third ranges have
    # remaining values
    while ((i < mid1) and (k < high)):
        if(gArray[i] < gArray[k]):
            destArray[l] = gArray[i]
            l += 1
            i += 1
        else:
            destArray[l] = gArray[k]
            l += 1
            k += 1
 
    # Copy remaining values from the first range
    while (i < mid1):
        destArray[l] = gArray[i]
        l += 1
        i += 1
 
    # Copy remaining values from the second range
    while (j < mid2):
        destArray[l] = gArray[j]
        l += 1
        j += 1
 
    # Copy remaining values from the third range
    while (k < high):
        destArray[l] = gArray[k]
        l += 1
        k += 1
 
 
""" Performing the merge sort algorithm on the
given array of values in the rangeof indices
[low, high). low is minimum index, high is
maximum index (exclusive) """
 
 
def mergeSort3WayRec(gArray, low, high, destArray):
    # If array size is 1 then do nothing
    if (high - low < 2):
        return
 
    # Splitting array into 3 parts
    mid1 = low + ((high - low) // 3)
    mid2 = low + 2 * ((high - low) // 3) + 1
 
    # Sorting 3 arrays recursively
    mergeSort3WayRec(destArray, low, mid1, gArray)
    mergeSort3WayRec(destArray, mid1, mid2, gArray)
    mergeSort3WayRec(destArray, mid2, high, gArray)
 
    # Merging the sorted arrays
    merge(destArray, low, mid1, mid2, high, gArray)
 
 
def mergeSort3Way(gArray, n):
    # if array size is zero return null
    if (n == 0):
        return
 
    # creating duplicate of given array
    fArray = []
 
    # copying elements of given array into
    # duplicate array
    fArray = gArray.copy()
 
    # sort function
    mergeSort3WayRec(fArray, 0, n, gArray)
 
    # copy back elements of duplicate array
    # to given array
    gArray = fArray.copy()
 
    # return the sorted array
    return gArray
 
 
data = [45, -2, -45, 78, 30, -42, 10, 19, 73, 93]
data = mergeSort3Way(data, 10)
print("After 3 way merge sort: ", end="")
for i in range(10):
    print(f"{data[i]} ", end="")
 
# This code is contributed by Susobhan Akhuli


C




// C Program to perform 3 way Merge Sort
#include <stdio.h>
 
/* Merge the sorted ranges [low, mid1), [mid1,mid2)
and [mid2, high) mid1 is first midpoint
index in overall range to merge mid2 is second
midpoint index in overall range to merge*/
void merge(int gArray[], int low, int mid1, int mid2, int high, int destArray[]) {
    int i = low, j = mid1, k = mid2, l = low;
 
    // Choose smaller of the smallest in the three ranges
    while ((i < mid1) && (j < mid2) && (k < high)){
        if(gArray[i] < gArray[j]){
            if(gArray[i] < gArray[k])
                destArray[l++] = gArray[i++];
            else
                destArray[l++] = gArray[k++];
        }
        else{
            if(gArray[j] < gArray[k])
                destArray[l++] = gArray[j++];
            else
                destArray[l++] = gArray[k++];
        }
    }
 
    // Case where first and second ranges
    // have remaining values
    while ((i < mid1) && (j < mid2)) {
        if(gArray[i] < gArray[j])
            destArray[l++] = gArray[i++];
        else
            destArray[l++] = gArray[j++];
    }
 
    // case where second and third ranges
    // have remaining values
    while ((j < mid2) && (k < high)) {
        if(gArray[j] < gArray[k])
            destArray[l++] = gArray[j++];
        else
            destArray[l++] = gArray[k++];
    }
 
    // Case where first and third ranges have
    // remaining values
    while ((i < mid1) && (k < high)) {
        if(gArray[i] < gArray[k])
            destArray[l++] = gArray[i++];
        else
            destArray[l++] = gArray[k++];
    }
 
    // Copy remaining values from the first range
    while (i < mid1)
        destArray[l++] = gArray[i++];
 
    // Copy remaining values from the second range
    while (j < mid2)
        destArray[l++] = gArray[j++];
 
    // Copy remaining values from the third range
    while (k < high)
        destArray[l++] = gArray[k++];
}
 
 
/* Performing the merge sort algorithm on the
given array of values in the rangeof indices
[low, high). low is minimum index, high is
maximum index (exclusive) */
void mergeSort3WayRec(int gArray[], int low, int high, int destArray[]) {
    // If array size is 1 then do nothing
    if (high - low < 2)
        return;
 
    // Splitting array into 3 parts
    int mid1 = low + ((high - low) / 3);
    int mid2 = low + 2 * ((high - low) / 3) + 1;
 
    // Sorting 3 arrays recursively
    mergeSort3WayRec(destArray, low, mid1, gArray);
    mergeSort3WayRec(destArray, mid1, mid2, gArray);
    mergeSort3WayRec(destArray, mid2, high, gArray);
 
    // Merging the sorted arrays
    merge(destArray, low, mid1, mid2, high, gArray);
}
 
void mergeSort3Way(int gArray[], int n){
    // if array size is zero return null
    if (n == 0)
        return;
 
    // creating duplicate of given array
    int fArray[n];
 
    // copying elements of given array into
    // duplicate array
    for (int i = 0; i < n; i++)
        fArray[i] = gArray[i];
 
    // sort function
    mergeSort3WayRec(fArray, 0, n, gArray);
 
    // copy back elements of duplicate array
    // to given array
    for (int i = 0; i < n; i++)
        gArray[i] = fArray[i];
}
 
// Driver Code
int main(){
    int data[] = {45, -2, -45, 78, 30, -42, 10, 19, 73, 93};
    mergeSort3Way(data,10);
    printf("After 3 way merge sort: ");
    for (int i = 0; i < 10; i++)
        printf("%d ",data[i]);
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


Output

After 3 way merge sort: -45 -42 -2 10 19 30 45 73 78 93 

How the above code works?

Here, we first copy the contents of data array to another array called fArray. Then, sort the array by finding midpoints that divide the array into 3 parts and called sort function on each array respectively. The base case of recursion is when size of array is 1 and it returns from the function. Then merging of arrays starts and finally the sorted array will be in fArray which is copied back to gArray. 

Time Complexity: In case of 2-way Merge sort we get the equation: T(n) = 2T(n/2) + O(n) 
Similarly, in case of 3-way Merge sort we get the equation: T(n) = 3T(n/3) + O(n) 
By solving it using Master method, we get its complexity as O(n log 3n). 

Although time complexity looks less compared to 2 way merge sort, the time taken actually may become higher because number of comparisons in merge function go higher. Please refer Why is Binary Search preferred over Ternary Search? for details.

Auxiliary Space Complexity: The space complexity of 3-way merge sort is same as 2-way merge sort: O(n)

Similar article: 3 way Quick Sort

 



Last Updated : 03 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads