34.3 Theoretical Bounds on Sorting

What is the best time we can get for sorting?

So far, the fastest sorts we have gone over have had Θ(NlogN)\Theta(N*logN)as the worse case time. This begs the question; can we get a sorting algorithm that would have a better asymptotic analysis?

Let's say that we have "the algorithm ultimate comparison sort" known as TUCS which is the fastest possible comparison sort. We don't know the details of this yet-to-be-discovered algorithm as it has not been discovered yet. Let's additionally say that R(N)R(N)is the worst-case runtime of TUCS and give ourselves the challenge of finding the best Θ\Theta and OO bounds for R(N)R(N). (This might seem a little odd since we really don't know the details of it, but we can find R(N)R(N)).

As a starting point, we can go ahead and have the OO bound of R(N)R(N) be O(NlogN)O(N*logN). Why? If this is the fastest sorting algorithm it must be at least just as good as the algorithms we already have! Thus it must at least be as good as Mergesort's worst case as otherwise we would have a contradiction in calling this algorithm TUCS.

What about the starting point for the Ω\Omega bound of R(N)R(N)? Since we don't quite know any details about it we can start by giving it the best possible runtime of Ω(1)\Omega(1). But can we make this tighter?

A Game of Cat and Mouse (and Dog)

Let's say we place Tom the Cat, Jerry the Mouse, and Spike the Dog in opaque soundproof boxes labeled A, B, and C. We want to figure out which is which using a scale.

Let's say we knew that Box A weighs less than Box B, and that Box B weighs less than Box C. Could we tell which is which?

The answer turns out to be yes!

We can find a sorted order by just considering these two inequalities. What if Box A weighs more than Box B, and that Box B weighs more than Box C? Could we still find out which is which?

The answer turns out to be yes! So far, we have been able to solve this game with just two inequalities! Let's go ahead and try a third case scenario. Could we know which is which if Box A weighs less than Box B, but Box B weighs more than Box C?

The answer turns out to be no. It turns out to have two possible orderings:

  • a: mouse, c: cat, b: dog (sorted order: acb)

  • c: mouse, a: cat, b: dog (sorted order: cab)

So while we were on a really great streak of solving the game with only two inequalities, we will need a third to solve all possibilities of the game. If we add the inequality a < c then this problem goes away and becomes solvable.

Now that we've created this table, we can create a tree to help us solve this sorting game of cat and mouse (and dog).

But how can we make this generalizable for all sorting? We know that each leaf in this tree is a unique possible answer to how boxes should be sorted. So the number of ways the boxes can be sorted should turn out to be the number of leaves in the tree. That then begs the question of how many ways can N boxes be sorted? The answer turns out to be N!N! ways as there are N!N! permutations of a given set of elements.

So how many questions do we need to ask in order to know how to sort the boxes? We would need to find the number of levels we must go through to get our answer in the leaf. Since it's a binary tree the minimum amount levels turn out lg(N!)lg(N!)levels to reach a leaf. (Note that lglg means (log base 2)).

So, using this game we have found that we must ask at least lg(N!)lg(N!) questions about inequalities to find a proper way to sort it. Thus our lower bound for R(N)R(N) is Ω(lg(N!))\Omega(lg(N!)).

Last updated