Merge Sort

·

1 min read

Sorting technique that uses Divide and Conquer method. Recursion is used. Merge sort is mostly used in the linked list. And when the size of the array is large.

Complexity

Time complexity

Worst case,Best case -O(N * log N)

Space Complexity - O(N)

What is Recursion? A function that calls itself

Recursion function of factorial of a number.

To find fact(4) - we have to get output from fact(3) -then f(2) --so on. When it reaches fact(1) . The value of that is sent to the function called it and it is removed from the stack. Example fact(2) is calling fact(1) .So fact(1) will send its output fact(2) and fact(1) is removed from stack

Steps

Divide the array into subarrays, subarrays are sorted and merged

merge sort algorithm

  1. Divide into Subarray

The array into divided by finding the mid value

mid = array length / 2

Now you have left Subarray which is from (Start index, Mid value)

Right subarray -(Midvalue +1 , end index)

  1. Sorting

Sorting takes place in both subarrays by Recursion.

  1. Merging

After sorting the subarray, the next step is to merge the subarray into the Sorted array

Practice Merge problems - https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/practice-problems/

For more reference - https://www.geeksforgeeks.org/merge-sort/

https://www.youtube.com/watch?v=iKGAgWdgoRk&list=PL9gnSGHSqcnr_DxHsP7AW9ftq0AtAyYqJ&index=31