Divide and Conquer

Merge Sort


Quicksort Running Time

Randomized Quicksort Running Time

Running Times

n = 20k 40k 80k 160k 320k 1,280k 2,560k
Insertion 3s 12s 54s 4.2m 19.5m > 5h? > 20h?
Selection 6s 25s 1.9m 9m 38m > 10h? > 40h?
QuickSort 0.03s 0.05s 0.11s 0.23s 0.49s 2.10s 4.34s
MergeSort 0.05s 0.11s 0.22s 0.46s 0.95s 4.12s 8.57s

s = seconds, m = minutes, h = hours
red numbers are estimates

Tweaking MergeSort

We can change MergeSort so it calls Insertion Sort, instead of recursive calls to itself, for small arrays.

These running times were collected for MergeSort on 1,000,000 items with different switch over points. All times in seconds.

switchover = 5 10 15 20 30 50 100 500
MergeSort 2.319 2.144 2.084 2.074 2.081 2.083 2.222 5.171