# What Is Merge Sort Algorithm in Data Structure?

//

Angela Bailey

In data structures, merge sort is an efficient algorithm used for sorting elements. It follows the divide-and-conquer approach to break down the task into smaller subproblems, solve them, and then merge the results back together to obtain the final sorted list.

## How does Merge Sort work?

The merge sort algorithm can be divided into two main steps:

1. Divide: The input list is divided into two halves recursively until each subproblem contains a single element or no elements at all.
2. Merge: The sorted sublists are then merged back together in a sorted order until we get a single sorted list as the final result.

### Divide Step

In the divide step of merge sort, the input list is divided into two halves. This process continues until each subproblem contains either one element or no elements at all. To divide the list, we follow these steps:

• Step 1: Find the middle point of the list by calculating (start + end) / 2.
• Step 2: Recursively divide the left half of the list from start to mid.
• Step 3: Recursively divide the right half of the list from mid+1 to end.

### Merge Step

In the merge step of merge sort, we take two sorted sublists and merge them into a single sorted sublist. To merge two lists, we follow these steps:

• Step 1: Create an auxiliary array that will store the merged result.
• Step 2: Initialize three pointers – one for the left sublist, one for the right sublist, and one for the merged result.
• Step 3: Compare the elements at the current positions of the left and right sublists.
• Step 4: Place the smaller element in the merged result array and move the respective pointer forward.
• Step 5: Repeat steps 3-4 until all elements have been placed in the merged result array.
• Step 6: Copy any remaining elements from either sublist into the merged result array.

• Efficiency: Merge sort has a time complexity of O(n log n), which makes it efficient for large datasets.
• Stability: Merge sort is a stable sorting algorithm, meaning that it preserves the relative order of equal elements in the sorted list.