Merge Sort
Merge Sort is a divide-and-conquer algorithm that divides the array into halves, recursively sorts them, and then merges the sorted halves.
IntermediateTime: O(n log n)Space: O(n)Stable
Take QuizCurrent Step
Press Play to start the visualization
## How Merge Sort Works
Merge Sort is a divide-and-conquer algorithm that divides the array into halves, recursively sorts them, and then merges the sorted halves back together.
### Algorithm Steps:
1. Divide the array into two halves
2. Recursively sort each half
3. Merge the two sorted halves into one sorted array
4. Base case: Arrays of size 0 or 1 are already sorted
### Key Characteristics:
- **Stable**: Equal elements maintain their relative order
- **Predictable**: Always O(n log n) regardless of input
- **Extra space**: Requires O(n) auxiliary space for merging
Time & Space Complexity
| Case | Time Complexity |
|---|---|
| Best | O(n log n) |
| Average | O(n log n) |
| Worst | O(n log n) |
| Space | O(n) |
Practice Problems
10 problems81 total points
1
Basic Merge Sort
Beginner5 pts
2
Merge Two Sorted Arrays
Beginner5 pts
3
Count Merge Operations
Beginner5 pts
4
In-Place Merge
Intermediate8 pts
5
Count Inversions
Intermediate8 pts
6
Merge K Sorted Arrays
Advanced10 pts
7
Sort Linked List
Advanced10 pts
8
Reverse Pairs
Advanced10 pts
9
External Merge Sort
Advanced10 pts
10
Count Smaller After Self
Advanced10 pts