Sorting Algorithms
📋 Quick Reference
| Algorithm | Time (Best) | Time (Avg) | Time (Worst) | Space | Stable |
|---|---|---|---|---|---|
| Bubble Sort | O(n) | O(n²) | O(n²) | O(1) | Yes |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | No |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | Yes |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | No |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No |
🎮 Interactive Visualizer
Watch different sorting algorithms race side-by-side:
Loading visualizer...
- Switch between algorithms using the selector
- Compare how many swaps/comparisons each makes
- Notice which algorithms are faster on nearly-sorted data
Side-by-Side Comparison
Loading visualizer...
🔧 Algorithm Implementations
Bubble Sort
JAVA(14 lines)CodeLoading syntax highlighter...
Selection Sort
JAVA(13 lines)CodeLoading syntax highlighter...
Insertion Sort
JAVA(12 lines)CodeLoading syntax highlighter...
Merge Sort
JAVA(27 lines)CodeLoading syntax highlighter...
Quick Sort
JAVA(23 lines)CodeLoading syntax highlighter...
Java Standard Library
JAVA(16 lines)CodeLoading syntax highlighter...
📊 When to Use Which Algorithm
Small Arrays (n < 50)
- Simple, low overhead
- Very fast for small n
- Java's Arrays.sort uses it for small subarrays
General Purpose
- Quick Sort: Fastest in practice for primitives
- Merge Sort: Stable, guaranteed O(n log n)
- TimSort: Hybrid, exploits existing order
Memory Constrained
- Both use O(1) or O(log n) extra space
- Heap Sort is guaranteed O(n log n)
Nearly Sorted Data
- O(n) for nearly sorted data
- TimSort specifically designed for this
Stability Required
- Maintains relative order of equal elements
- Critical for multi-key sorting
🧩 Stability Explained
JAVA(10 lines)CodeLoading syntax highlighter...
⚠️ Common Pitfalls
1. Wrong Algorithm Choice
JAVA(5 lines)CodeLoading syntax highlighter...
2. Quick Sort Worst Case
JAVA(5 lines)CodeLoading syntax highlighter...
3. Forgetting Stability Needs
JAVA(6 lines)CodeLoading syntax highlighter...
4. Modifying During Sort
JAVA(5 lines)CodeLoading syntax highlighter...
🎯 Interview Practice
Test your sorting algorithm knowledge with 10 curated interview questions:
Loading visualizer...
🎤 Interview Tips
"For primitives: Dual-Pivot QuickSort. For objects: TimSort (a hybrid of merge sort and insertion sort). TimSort is stable, QuickSort is not.
"For small arrays (n < 50), insertion sort is often fastest due to low overhead. Also useful when data is nearly sorted.
"On average, the pivot splits the array roughly in half, giving log n levels of recursion, each doing O(n) work total. Worst case (already sorted with bad pivot) degrades to O(n²).
"A hybrid algorithm combining merge sort and insertion sort. It identifies "runs" (sorted subsequences) and merges them efficiently. Optimized for real-world data that often has existing order.
📚 Series Navigation
Sorting, SortingComparison from @tomaszjarosz/react-visualizers