Merge Sort Visualizer
What is Merge Sort
Merge Sort is an efficient, stable, comparison-based sorting algorithm that follows the divide-and-conquer approach. It works by recursively dividing the unsorted list into sublists until each sublist contains a single element, then repeatedly merges these sublists to produce new sorted sublists until there is only one sorted list remaining.
How Does It Work
Divide Phase:
- Split the array into two halves: [38, 27, 43] and [3, 9, 82, 10]
- Recursively split each half until single elements remain
- Merge single elements into sorted pairs: [27, 38], [3, 43], [9, 82], [10]
- Merge pairs: [3, 27, 38, 43] and [9, 10, 82]
- Final merge: [3, 9, 10, 27, 38, 43, 82]
Original: [38, 27, 43, 3, 9, 82, 10] Divide: [38,27,43][3,9,82,10] Divide: [38][27,43] | [3,9][82,10] Divide: [38][27][43] | [3][9][82][10] Merge: [27,38][43] | [3,9][10,82] Merge: [27,38,43] | [3,9,10,82] Final: [3,9,10,27,38,43,82]
Algorithm Steps
- Divide:
- Find the middle point to divide the array into two halves
- Recursively call merge sort on the first half
- Recursively call merge sort on the second half
- Merge:
- Create temporary arrays for both halves
- Compare elements from each half and merge them in order
- Copy any remaining elements from either half
Time Complexity
- Best Case: O(n log n) (already sorted, but still needs all comparisons)
- Average Case: O(n log n)
- Worst Case: O(n log n) (consistent performance)
The log n factor comes from the division steps, while the n factor comes from the merge steps.
Space Complexity
Advantages
- Stable sorting (maintains relative order of equal elements)
- Excellent for large datasets (consistent O(n log n) performance)
- Well-suited for external sorting (sorting data too large for RAM)
- Easily parallelizable (divide steps can be done concurrently)
Disadvantages
- Requires O(n) additional space (not in-place)
- Slower than O(n²) algorithms for very small datasets due to recursion overhead
- Not as cache-efficient as some other algorithms (e.g., QuickSort)
Merge Sort is particularly useful when sorting linked lists (where random access is expensive) and is the algorithm of choice for many standard library sorting implementations when stability is required. It's also commonly used in external sorting where data doesn't fit in memory.
Visualize the divide-and-conquer approach of Merge Sort with recursive splitting and merging.
Main Array
Merge Sort Process
Visual Guide:
Steps:
- Divide array into halves recursively until single elements
- Merge adjacent subarrays in sorted order
- Continue merging until whole array is sorted