Algorithms

Sorting Algorithms

📋 Quick Reference

AlgorithmTime (Best)Time (Avg)Time (Worst)SpaceStable
Bubble SortO(n)O(n²)O(n²)O(1)Yes
Selection SortO(n²)O(n²)O(n²)O(1)No
Insertion SortO(n)O(n²)O(n²)O(1)Yes
Merge SortO(n log n)O(n log n)O(n log n)O(n)Yes
Quick SortO(n log n)O(n log n)O(n²)O(log n)No
Heap SortO(n log n)O(n log n)O(n log n)O(1)No
One-liner: Sorting arranges elements in order; choose algorithm based on data size, memory constraints, and stability needs.

🎮 Interactive Visualizer

Watch different sorting algorithms race side-by-side:

Loading visualizer...
Try these:
  1. Switch between algorithms using the selector
  2. Compare how many swaps/comparisons each makes
  3. Notice which algorithms are faster on nearly-sorted data

Side-by-Side Comparison

Loading visualizer...

🔧 Algorithm Implementations

Bubble Sort

JAVA(14 lines)
Code
Loading syntax highlighter...

Selection Sort

JAVA(13 lines)
Code
Loading syntax highlighter...

Insertion Sort

JAVA(12 lines)
Code
Loading syntax highlighter...

Merge Sort

JAVA(27 lines)
Code
Loading syntax highlighter...

Quick Sort

JAVA(23 lines)
Code
Loading syntax highlighter...

Java Standard Library

JAVA(16 lines)
Code
Loading syntax highlighter...

📊 When to Use Which Algorithm

Small Arrays (n < 50)

Use: Insertion Sort
  • Simple, low overhead
  • Very fast for small n
  • Java's Arrays.sort uses it for small subarrays

General Purpose

Use: Quick Sort (primitives) or Merge Sort/TimSort (objects)
  • Quick Sort: Fastest in practice for primitives
  • Merge Sort: Stable, guaranteed O(n log n)
  • TimSort: Hybrid, exploits existing order

Memory Constrained

Use: Heap Sort or Quick Sort
  • Both use O(1) or O(log n) extra space
  • Heap Sort is guaranteed O(n log n)

Nearly Sorted Data

Use: Insertion Sort or TimSort
  • O(n) for nearly sorted data
  • TimSort specifically designed for this

Stability Required

Use: Merge Sort or TimSort
  • Maintains relative order of equal elements
  • Critical for multi-key sorting

🧩 Stability Explained

Stable sort: Equal elements maintain their relative order
JAVA(10 lines)
Code
Loading syntax highlighter...

⚠️ Common Pitfalls

1. Wrong Algorithm Choice

JAVA(5 lines)
Code
Loading syntax highlighter...

2. Quick Sort Worst Case

JAVA(5 lines)
Code
Loading syntax highlighter...

3. Forgetting Stability Needs

JAVA(6 lines)
Code
Loading syntax highlighter...

4. Modifying During Sort

JAVA(5 lines)
Code
Loading syntax highlighter...

🎯 Interview Practice

Test your sorting algorithm knowledge with 10 curated interview questions:

Loading visualizer...

🎤 Interview Tips

Q: Which sorting algorithm does Java use?
"

For primitives: Dual-Pivot QuickSort. For objects: TimSort (a hybrid of merge sort and insertion sort). TimSort is stable, QuickSort is not.

Q: When would you use O(n²) algorithms?
"

For small arrays (n < 50), insertion sort is often fastest due to low overhead. Also useful when data is nearly sorted.

Q: How does QuickSort achieve O(n log n) average?
"

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²).

Q: What is TimSort?
"

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


Visualizers: Sorting, SortingComparison from @tomaszjarosz/react-visualizers