68
Chapter 4
I
Quicksort
You
usually ignore that constant, because if two algorithms have
different Big O times, the constant doesn’t matter.
Take binary search
and simple search, for example. Suppose both algorithms had these
constants.
You might say, “Wow! Simple search has a constant of 10
milliseconds,
but binary search has a constant of 1 second. Simple search is way
faster!” Now suppose you’re searching a list of 4 billion elements. Here
are the times.
As you can see, binary search is still way faster. That constant didn’t
make a difference at all.
But
sometimes the constant
can
make a difference. Quicksort versus
merge sort is one example. Quicksort has a smaller constant than
merge sort. So if they’re both O(
n
log
n
) time, quicksort is faster. And
quicksort is faster in practice because it hits
the average case way more
often than the worst case.
So now you’re wondering: what’s the average case versus the worst case?
Dostları ilə paylaş: