Aller au contenu principal
Interactive Algorithm Education

Visualize & Master Algorithms & Data Structures

Explore classic & modern sorting algorithms, efficient searching techniques, and interactive data structure visualizations — all with real-time step-by-step animation, comparisons, swaps, and Big-O metrics.

Sorting Visualizer

Bubble Sort

Speed 100ms
Size 20
Step Progress 0 / 0
Comparisons 0
Swaps / Shifts 0
Status Ready
Default
Comparing
Swapping
Pivot / Min
Sorted
⬡ Held key (ghost)
Step Explanation

Click 'Play' or 'Step Forward' to begin visualization.

Bubble Sort • Time: O(n²) • Space: O(1)
Pseudocode
        
History

Select an algorithm to see its history.

Timsort

Advanced (4/5) ~45 minutes Hybrid sorting strategy Natural run detection Insertion sort on small runs Balanced merging of runs Prereqs: Insertion Sort, Merge Sort
Quick Reference

Timsort

Timsort is a real-world hybrid algorithm combining Insertion Sort (for small contiguous blocks called runs) and Merge Sort (to combine sorted runs). Used by Python, Java, and V8.

Difficulty: Advanced (4/5) Stablesorting

Complexity

Best Time
O(n)
Average Time
O(n log n)
Worst Time
O(n log n)
Space
O(n)

When to Use

General-purpose real-world sorting of heterogeneous real-world data (default algorithm in Python, Java, Rust, V8 JS).

Pros

  • Linear O(n) performance on real-world partially sorted arrays
  • Guaranteed O(n log n) worst case
  • Stable sorting behavior

Cons

  • Requires O(n) auxiliary space for merging
  • Higher implementation complexity than simple algorithms

History

Timsort was developed by Tim Peters in 2002 for the Python programming language. It was adopted by Java's Arrays.sort in 2011 and by the V8 JavaScript engine in 2018.

Timsort is the sort the real world actually runs: Python’s default, Java’s Arrays.sort for objects, and the JavaScript V8 engine’s array sort.

It is a hybrid — Insertion Sort for small chunks, Merge Sort for combining them — with one clever twist: it first looks for natural runs (already-sorted stretches) and exploits them.

Because real-world data is rarely random (slightly sorted lists, appended rows, merged logs), Timsort hits its near-linear best case far more often than any pure sort.

How It Works

  1. Detect runs: scan the array, finding maximal already-sorted (ascending) or strictly-descending segments.
  2. Normalize run size: if a run is shorter than a minimum size (e.g., 32–64, or 4 in the demo), extend it using Insertion Sort.
  3. Merge: combine adjacent runs with a balanced merge, using a stack to keep run lengths roughly powers of two.
  4. Repeat merging until one sorted run remains.

Key Insight

Two ideas make Timsort special: run detection and adaptive merging.

  • Pure merge sort splits input blindly in half, paying O(n log n) even on sorted data.
  • Timsort sees that big sorted stretches already exist and treats each as a completed run — sorting an already-sorted array is essentially one linear pass.

The run-stack also merges runs only when lengths are balanced, avoiding the pathological merges of a naive binary split.

Worked Example

Sort [5, 3, 8, 1, 2, 9, 7] with the demo’s run size of 4:

  1. Detect runs: [5, 3] is descending → flip to ascending [3, 5]; [8] follows → run [3, 5, 8] (length 3); [1, 2, 9, 7] → detect [1, 2, 9] ascending then 7 breaks it → run [1, 2, 7, 9] after insertion-sorting 7.
  2. Merge run 1 ([3, 5, 8]) with run 2 ([1, 2, 7, 9]):
    • Compare heads: 1, 2, 3, 5, 7, 8, 9 → [1, 2, 3, 5, 7, 8, 9]

Result: [1, 2, 3, 5, 7, 8, 9]. In the visualizer, watch runs get merged in a balanced tree — each merge is the two-pointer walk you know from Merge Sort.

Edge Cases & Pitfalls

  • Already sorted input — one long run → nearly O(n): Timsort’s best case.
  • Reverse sorted input — one long descending run, flipped in linear time, then a single merge pass: also near O(n).
  • Random input — many tiny runs: the merge tree does the full O(n log n), no worse than a good comparison sort.
  • Duplicates — Timsort is stable: runs preserve order and the merge keeps left-run elements first.
  • Memory — requires O(n) auxiliary space for merging, like merge sort.

Comparison With Other Sorts

ScenarioTimsortQuick SortMerge Sort
Real-world (semi-sorted) dataNear O(n)O(n log n)O(n log n)
Worst caseO(n log n)O(n²) bad pivotsO(n log n)
StabilityStableUnstableStable
AdoptionPython, Java, V8, RustC, Go (Introsort)External sort

Applications

  • Default object sort in Python, Java, Rust, and JavaScript engines
  • Sorting data with structure — append-heavy logs, merged feeds, timestamped streams
  • Any workload where “mostly sorted” inputs are plausible, because it detects and exploits them

Practice Trajectory

  1. Identify the natural runs in [2, 5, 1, 4, 8, 7, 3] by hand.
  2. Explain why a descending run can be converted to ascending in linear time.
  3. Trace the merge of runs [1, 4, 7] and [2, 5, 8] with the two-pointer walk.
  4. Argue why Timsort is near-linear on already-sorted data while pure merge sort is not.
  5. Implement the demo’s simplified version (run size 4 + insertion + merge) and compare it against plain merge sort on a sorted input.