3 way merge sort in c

This article discusses 3 ways to merge sort in c. In merge sort, the array is recursively split into two parts, sorted, and finally merged.

The merge sort variant is treated as a 3-way merge sort that splits the array into three parts instead of splitting it into two parts. Merge sort recursively splits an array into half-sized subarrays. Similarly, a three-way merge sort decomposes an array into subarrays of one-third the size.

In merge sort, the array is recursively split into two parts, sorted, and finally merged. A variant of merge sort is called 3-way merge sort, where instead of splitting the array into two parts, he splits it into three parts.

Examples of the merge sort: The example of merge sort is given below -

The time complexity of a three-way merge sort is nlog3n.

Example 1: Here, we give an example of 3 way to merge sort in c. The example is given below -

Result: Now we compile the above program, and after successful compilation, we run it. Then the result is given below -

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 first replica the contents of the statistics array into every other array called fArr. Then type the array by locating the midpoint that divides the array into three elements and calls the type characteristic on every array. The basic case of recursion is when an array has size 1 and is returned from a function. Then the array merging starts, and finally, the sorted array is in fArr and copied to gArr.

The time complexity of the merge sort:

Three-way merge sort equation is: T(n) = 2T(n/2) + O(n)

Similarly, for a three-way merge sort, we have: T( n) = 3T(n/3) + O(n)

Solving with the master method, its complexity is O(n log 3n).

Time complexity appears less than a two-way merge sort, but the more comparisons in the merge function, the more time it might take in practice.

So, in this article, we briefly discuss 3 ways to merge sort in c. The merge sort variant is treated as a 3-way merge sort that splits the array into three parts instead of splitting it into two parts. We also give an example that is related to this topic.