Last Updated: 2 Dec, 2020

# Merge Sort

Easy

## Problem statement

#### Example :

``````Merge Sort Algorithm -

Merge sort is a Divide and Conquer based Algorithm. It divides the input array into two-parts, until the size of the input array is not ‘1’. In the return part, it will merge two sorted arrays a return a whole merged sorted array.
``````

``````The above illustrates shows how merge sort works.
``````
##### Note :
``````It is compulsory to use the ‘Merge Sort’ algorithm.
``````
##### Input format :
``````The first line of input contains an integer ‘T’ denoting the number of test cases.
The next 2*'T' lines represent the ‘T’ test cases.

The first line of each test case contains an integer ‘N’ which denotes the size of ‘ARR’.

The second line of each test case contains ‘N’ space-separated elements of ‘ARR’.
``````
##### Output Format :
``````For each test case, print the numbers in non-descending order
``````
##### Note:
``````You are not required to print the expected output; it has already been taken care of. Just implement the function.
``````
##### Constraints :
``````1 <= T <= 50
1 <= N <= 10^4
-10^9 <= arr[i] <= 10^9

Time Limit : 1 sec
``````

## Approaches

### 01 Approach

The basic idea is that we divide the given ‘ARR’ into two-part call them ‘leftHalves’ and ‘rightHalves’ and call the same function again with both the parts. In the end, we will get sorted ‘leftHaves’ and sorted ‘righthalves’ which we merge both of them and return a merged sorted ‘ARR’.

We implement this approach with a divide and conquer strategy.

Here is the algorithm :

1. Divide ‘ARR’ into two-part ‘leftHalves’ and ‘rightHalves’ and the size of both parts are almost equal means ‘leftHalves’ can have one size extra comparing to ‘rightHalves’
• Recursively solve for ‘leftHalves’
• Recursively solve for ‘rightHalves’
2. In the recursive part, every time we will get some part of ‘ARR’. Then divide it into two parts until the size of each subarray is not equal to 1.
3. In the return part, we get two sorted arrays ‘leftHalves’ and ‘rightHalves’ using recursion.
4. After getting both sorted parts, we merge both of them in such a way so that we get a merged sorted array.

MERGE() function :

1. Suppose we have two sorted arrays ‘leftHalves’ and ‘rightHalves’ then we merge both of them into ‘mergedArr’
2. Currently, we have two pointers ‘ptrLeft’ and ‘ptrRight’, and both are pointing to starting indices of ‘leftHalves’ and ‘rightHalves’.
• If ‘leftHalves[ptrLeft] < rightHalves[ptrRight]’ then add ‘leftHalves[ptrLeft]’ in ‘mergeArr’ and increase ‘ptrLeft’ by one.
• Else add ‘rightHalves[ptrRight]’ in ‘mergeArr’ and increase ‘ptrRight’ by one.
3. Add remaining elements from ‘leftHalves’ and ‘rightHalves’.
4. Copy ‘mergeArr’ elements to ‘ARR’.
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}