# What is Merge Sort?

## Introduction

Sorting algorithms are a fundamental part of data structure or we can say whole computer science. And in the era of sorting, one of the most popular and most accessible sorting algorithms is merge sort. Which uses the divide and conquer rule to sort an array.

In this article we are going to learn what is merge sort, how it performs, what are the functions, etc.

## What is Merge Sort?

Merge sort is nothing but a recursive algorithm that uses a divide-and-conquer rule to sort an array. It divides the input into two halves until and unless the subarray size is 0 or 1. Then it will merge the array by comparing the first element with each sub-array and it will be in a loop until all the elements have been added and sorted.

The divide and conquer strategy of merge sort is what makes it efficient for large data sets. We have a number of sorting algorithms in the data structure but have you ever thought about why we need merge sort algorithm? the biggest advantage of this sorting algorithm is pretty simple. Yes, this algorithm has a time complexity of O(n log n). And this is one of the easiest algorithms to sort large data.

## How Merge Sort Works?

To learn the process of this let’s consider an array A[] = {12 , 10 , 7 , 11 , 75 , 99 , 2}.

### Step 1:

First of all, to sort the array first check if the left index is smaller than the right index. If yes, then calculate the mid by using the given formula:

### Step 2:

Observe an array, if the left index is smaller than the right index then calculate the mid. Then divide an array from the mid-index.

### Step 3:

Following step 2 divide an array until the subarray size becomes 0 or 1.

### Step 4:

Now check the value of the 0th and 1st indexes, if the 0th index value is greater than the 1st index then swap them and merge both values.

### Step 5:

Now check the 2nd and 3rd index values, if the 2nd index value is greater than the 3rd index then swap them and merge both values. or else just merge them.

### Step 6:

Now to merge the 0th and 1st index values with the 2nd and 3rd index values, compare the 0th value with the 2nd and 3rd index values, if it is greater then swap them. Do the same for 1st, the 2nd, and 3rd also and merge them.

### Step 7:

Repeat the 6th step for the second half array. then merge them until all the values have been added and sorted in an array.

## Implementation of Merge Sort:

Implementing merge sort requires knowledge of basic programming concepts such as recursion and arrays.

### Algorithm of Merge Sort:

``````step 1: start

step 2: declare array and L, R, mid variable

step 3: perform merge function.
if L > R
return
mid= (L+R)/2
mergesort(array, L, mid)
mergesort(array, mid+1, R)
merge(array, L, mid, R)

step 4: Stop``````

## Conclusion

As you can that how easy is to implement merge sort. Overall, it is a reliable and efficient sorting algorithm that can be used for a variety of applications. It is particularly useful for sorting large datasets where performance is a priority.