Who would disagree that the run-time of mergesort is
However, if you try to understand how to formalize what it means for a sort
to run in
In this post, I’ll look at some of
the most common computational models used in both practice and theory, and
find out that they’re all flawed in one way or another, and in fact in all
of them either mergesort doesn’t run in
Model 1: RAM is fixed-sized words
First up is a pragmatic model. Here, every item is assumed to fit into a 64-bit word of RAM.
This has an immediate problem: in a model where every item must fit into a 64-bit
word, there’s only
Another, more subtle, problem here is that the registers of our machine must also be 64-bit. That means that there’s a finite (albeit huge) range of memory available to programs, which means that we can’t even represent large arrays.
This model isn’t abstract enough, and the constraint that everything fits in a single word is too restrictive. Since what we’d do in practice is to span large data across multiple words, let’s consider a model that’s like that.
Model 2: Data spans multiple fixed-sized words.
Here, we allow data to be stored across multiple fixed-sized words, just like a real computer. In this model,
to have an array of
Now, when we try mergesort we get that each comparison takes
Again like the fixed-sized word model, we have the additional subtlety about how registers work.
They need to be able to store arbitrarily large integers, and it’s not obvious how to design the machine
so that adding
Model 3: RAM is variable-sized words.
This model (a common one in the study of algorithmic complexity, and is also called the transdichotomous
model) models memory as words each of
This model starts well: merge sort runs in
Model 4: RAM is arbitrary integers.
This model allows each memory cell to contain an arbirarily large integer. It’s already obvious that this model isn’t going to satisfy us since it’s strictly more powerful than model 3. However, even though this model has diverged from what we think of as a computer, it’s surprising how powerful this model is.
A paper by Arnold Schönhage “On the power of random access machines” (2005) shows that in this model, any PSPACE problem can be solved in polynomial time. Since PSPACE includes all of NP, this is quite a feat. While in no way comparable to the paper, a hint to how to use arbitrary integer arithmetic to perform parallel computation is my blog post on computing Fibonacci numbers.
We could try to adjust the costs of arithmetic to be proportional to the number of bits used (called
the “bit cost” model). That fixes the PSPACE
Conclusion
Every one of our models we’ve looked at has either failed to allow merge sort
to work in
Even though the gap between complexity theory and its application to programming is
wider than it looks, it’d be silly to write off complexity theory as useless.
In practice it works well and has good predictive
power, perhaps because the calculations that
we do to compute complexity approximate a more bounded notion, where “this algorithm is