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.

Merge Sort

Intermediate (3/5) ~45 minutes Divide and conquer Recursive splitting to single elements Linear-time merging Guaranteed O(n log n) with stability Prereqs: Recursion, Arrays
Quick Reference

Merge Sort

Merge Sort is a classic divide-and-conquer algorithm. It recursively splits the array into single-element sub-lists, then merges adjacent sorted lists back together in sorted order.

Difficulty: Intermediate (3/5) Stablesorting

Complexity

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

When to Use

When guaranteed O(n log n) worst-case time complexity and stability are required, or when sorting linked lists and external files.

Pros

  • Guaranteed O(n log n) efficiency for Best, Average, and Worst cases
  • Stable sort: preserves relative ordering of equal items
  • Highly efficient for linked list data structures and external sorting

Cons

  • Requires O(n) additional space for buffer arrays during merging
  • Higher memory copy overhead compared to in-place sorting routines

History

Merge Sort was invented by John von Neumann in 1945, making it one of the oldest computer sorting algorithms still in widespread use today.

Merge Sort is the divide-and-conquer workhorse: split a list in half, sort each half, then merge the two sorted halves back together. Because the halves are already sorted, the merge is a fast two-pointer walk.

The guarantee: O(n log n) on every input — no unlucky pivots, no quadratic worst case — at the price of O(n) extra memory for merge buffers.

How It Works

  1. Divide: find the midpoint of the array and recursively split it into left and right halves.
  2. Conquer: keep splitting until each sub-array contains one element (the base case — a single element is trivially sorted).
  3. Combine: merge two sorted sub-arrays by comparing their smallest remaining elements and appending the smaller into a target array.
  4. Repeat the merge up the recursion tree until the entire array is unified.

Key Insight

The merge step does all the sorting — splitting only creates the sorted pieces. Merging two sorted lists of total size k costs O(k), and the recursion produces log n levels, each touching all n elements.

That balance — n elements per level × log n levels — is exactly O(n log n). Merge Sort is also stable, and the natural choice for sequentially-accessed data (linked lists, tape drives, external files).

Worked Example

Sort [5, 3, 8, 1, 2] with Merge Sort:

  1. Split: [5, 3, 8, 1, 2] → [5, 3] + [8, 1, 2]
  2. Split again: [5, 3] → [5] + [3]; [8, 1, 2] → [8] + [1, 2] → [1] + [2]
  3. Merge [5] + [3] → [3, 5]
  4. Merge [1] + [2] → [1, 2]; then [8] + [1, 2] → [1, 2, 8]
  5. Merge [3, 5] + [1, 2, 8] → [1, 2, 3, 5, 8]

Result: [1, 2, 3, 5, 8]. In the visualizer, the two-pointer merge walks each pair of sorted runs — watch the smaller of the two “heads” get written to the output each step.

Edge Cases & Pitfalls

  • Single element or empty — trivially sorted: the recursion base case.
  • Duplicates — the merge uses <=, so equal elements from the left run are written first, preserving stability.
  • Large arrays — needs O(n) auxiliary space. If memory is tight, consider an in-place merge variant or Heapsort.
  • Stack depth — recursive splitting uses O(log n) call stack. Deep recursion on huge inputs can overflow; an iterative (bottom-up) version avoids this.

Comparison With Other Sorts

ScenarioMerge SortQuick SortHeap Sort
Worst caseO(n log n) guaranteedO(n²) on bad pivotsO(n log n)
StabilityStableUnstableUnstable
SpaceO(n) auxiliaryO(log n) stackO(1)
Best whenStability + guaranteed boundIn-memory speedO(1) space guarantee

Applications

  • Sorting linked lists (merge is sequential-access friendly; no random indexing needed)
  • External sorting of files too large for memory (chunk → sort → merge)
  • Library sort implementations where stability is required (e.g., Collections.sort in Java for objects)
  • Real-time systems needing a predictable worst case

Practice Trajectory

  1. Hand-split and re-merge [6, 1, 4, 2, 9, 3], drawing the full recursion tree.
  2. Implement merge(left, right) with two pointers and confirm it is linear.
  3. Trace the merge of [1, 4, 7] and [2, 5, 8] step by step.
  4. Explain why the total work is n log n even when the input is already sorted.
  5. Implement an iterative (bottom-up) version that avoids recursion.