CSAlgorithms & ComplexityDiagramBeginner
Merge Sort vs Quick Sort
KO: 병합 정렬 vs 퀵 정렬·EN: Merge Sort vs Quick Sort·JA: マージソート vs クイックソート
Comparing Merge Sort (guaranteed O(N log N), stable, O(N) buffer) against Quick Sort (in-place, cache-friendly, fastest average runtime)
Table of Contents
Structure Diagram
Merge Sort vs Quick Sort Comparison
MERGEMerge Sort
Guaranteed O(N log N) · Stable sort
- Worst TimeO(N log N) — input-independent
- Extra SpaceO(N) — temporary merge buffer
- StabilityStable (preserves equal-key order)
- Best ForLinked lists, external disk sort, Timsort
QUICKQuick Sort
Fastest in practice · In-place & cache-friendly
- Worst TimeO(N²) on bad pivots (mitigated by Introsort)
- Extra SpaceO(log N) — in-place partition stack
- StabilityUnstable (may reorder equal keys)
- Best ForIn-memory primitive arrays, high cache locality
Quick Answer
What is Merge Sort vs Quick Sort?
Comparing Merge Sort (guaranteed O(N log N), stable, O(N) buffer) against Quick Sort (in-place, cache-friendly, fastest average runtime). It is a core concept in Algorithms & Complexity within CS.
Core Principles
Both Merge Sort and Quick Sort use Divide and Conquer, but place their work in opposite phases.
- Merge Sort: Splits trivially in half and does the real work while merging sorted halves into an
O(N)buffer. It guaranteesO(N log N)worst-case time and is stable. - Quick Sort: Partitions elements around a pivot in place, then recurses. Sequential memory access gives it superior CPU cache locality, making it faster on average despite an
O(N²)worst case on adversarial pivots.
Practical Takeaways
- Production runtimes use hybrids:
- Timsort (Python, Java objects, V8): Merge + Insertion Sort exploiting naturally ordered runs.
- Introsort / pdqsort (C++
std::sort, Rustsort_unstable): Quick Sort with fallback to Heap Sort to eliminateO(N²)worst cases.
- Stability matters for multi-key sorting: Sorting by department then by name requires a stable sort to preserve secondary order.
- External Sorting: Datasets larger than RAM rely on multi-way External Merge Sort.
Notable Examples
- Python & Java Object Sorting: Timsort built on Merge Sort principles.
- C++
std::sort& Goslices.Sort: Introsort and pdqsort built on Quick Sort. - Database
ORDER BYSpill-to-Disk: External Merge Sort for massive result sets.