Introduction
Merge Sort is a:
- Divide and Conquer
- sorting algorithm
The idea is:
- divide array into halves
- sort recursively
- merge sorted parts
Example:
Input:
5 2 4 1 3
Output:1 2 3 4 5
Explanation:
Array is divided into smaller parts and merged back in sorted order. This problem is one of the most important applications of:
Divide and Conquer Constraints
1 <= Array Size <= 10^5 Approach : Merge Sort Algorithm
Explanations:
Explanation:
The idea is:
- recursively divide array
- merge sorted halves
Steps:
- Find middle index.
- Divide array into two halves.
- Sort left half recursively.
- Sort right half recursively.
- Merge sorted arrays.
This approach:
- guarantees stable sorting
- works efficiently for large data
Dry Run
Array: 5 2 4 1 3
Divide:
5 2 | 4 1 3
5 | 2
4 | 1 3
1 | 3
Merge: 2 5 1 3 4 1 2 3 4 5
Practice :