Way Merge Sort In C

In the merge sort algorithm, the array undergoes a recursive process of division into two segments, which are then individually sorted before being merged back together. A modification of this approach is known as 3-way merge sort, where the array is divided into three parts instead of the usual two.

Examples of the merge sort algorithm are illustrated below -

Example

Input: 4, 8, -4, -9, 10, 55, 46, 70, -56, 78, 90, 67, 85, 20, 29
Output: -56 -9 -4 4 8 10 20 29 46 55 67 70 78 85 90
Input: 98, -67
Output: -67 98

The time complexity of a three-way merge sort algorithm is O(nlog3n).

In this instance, we provide a demonstration of three methods to implement merge sort in the C programming language. The illustration is presented below -

Example

#include <bits/stdc++.h>
usingnamespacestd;
voidmerge1(intgArr1[], intlow1, intmid1,intmid2, inthigh1, intdestArr1[])
{
   inti = low1, a = mid1, b = mid2, c = low1;
   while((i < mid1) && (a < mid2) && (b < high1))
{
      if(gArr1[i] < gArr1[j])
{
         if(gArr1[i] < gArr1[b])
{
            destArr1[c++] = gArr1[i++];
         }
         else {
               destArr1[c++] = gArr1[b++];
            }
      }
   else{
         if(gArr1[j] < gArr1[b]){
            destArr1[c++] = gArr1[a++];
         }
   else{
         destArr1[c++] = gArr1[b++];
      }
   }
}
while((i < mid1) && (a < mid2)){
   if(gArr1[i] < gArr1[a]){
      destArr1[c++] = gArr1[i++];
   }
   else{
     
destArr1[c++] = gArr1[a++];
   }
}
while((a < mid2) && (b < high1)){
   if(gArr1[a] < gArr1[b]){
   destArr1[c++] = gArr1[a++];
}
else{
      destArr1[c++] = gArr1[b++];
   }
}
while((i < mid1) && (b < high1))
{
         if(gArr1[i] < gArr1[b])
{
            destArr1[c++] = gArr1[i++];
         }
   else{
         destArr1[c++] = gArr1[b++];
      }
   }
   while(i < mid1)
   destArr1[c++] = gArr1[i++];
   while(a < mid2)
   destArr1[c++] = gArr1[b++];
   while(b < high)
   destArr1[c++] = gArr1[b++];
}
voidmergeSort3WayRec(intgArr1[], intlow1, inthigh1, intdestArr1[])
{
   if(high1 - low1 < 2)
   return;
   intmid1 = low1 + ((high1 - low1) / 3);
   intmid2 = low1 + 2 * ((high1 - low1) / 3) + 1;
   mergeSort3WayRec(destArr1, low1, mid1, gArr1);
   mergeSort3WayRec(destArr1, mid1, mid2, gArr1);
   mergeSort3WayRec(destArr1, mid2, high1, gArr1);
   merge(destArr1, low1, mid1, mid2, high1, gArr1);
}
voidmergeSort3Way(intgArr1[], intn1){
   if(n1 == 0)
   return;
   intfArr1[n];
   for(inti = 0; i < n1; i++)
   fArr1[i] = gArr1[i];
   mergeSort3WayRec(fArray1, 0, n, gArray1);
   for(inti = 0; i < n1; i++)
   gArr1[i] = fArr1[i];
}
int main(){
   intdata1[] = {4, 8, -4, -9, 10, 55, 46, 70, -56, 78, 90, 67, 85, 20, 29};
   mergeSort3Way(data1,10);
   cout<< "The result after the three way of merge sort is: ";
   for(inti = 0; i < 10; i++)
{
         cout<< data1[i] << " ";
      }
   return0;
}

Result: The next step involves compiling the aforementioned program. Following a successful compilation, we proceed to execute it. Subsequently, the outcome is displayed as follows -

Output

The result after the three way of merge sort is: -56 -9 -4 4 8 10 20 29 46 55 67 70 78 85 90

How does the above code work?

Here, we initially duplicate the data from the statistics array into alternate arrays named fArr. Next, we sort the array by identifying the midpoint that separates the array into three sections and invokes the sorting feature on each array. The fundamental scenario for recursion occurs when an array reaches a size of 1 and gets returned from a function. Subsequently, the process of merging arrays commences, culminating in the sorted array residing in fArr, which is then duplicated to gArr.

The time complexity of the merge sort:

The equation for three-way merge sort can be expressed as: T(n) = 2T(n/2) + O(n)

In the case of a three-way merge sort, the time complexity can be expressed as follows: T( n) = 3T(n/3) + O(n)

Solving using the master theorem results in a time complexity of O(n log 3n).

The time complexity seems lower compared to a two-way merge sort; however, as the number of comparisons increases within the merge function, the practical time taken may also increase.

So, within this guide, we will briefly explore three techniques for implementing merge sort in the C programming language. The approach we focus on is a modification of traditional merge sort known as a 3-way merge sort, dividing the array into three sections instead of the usual two. Furthermore, we provide an illustrative example relevant to this concept.

Input Required

This code uses input(). Please provide values below: