# 34.3 Theoretical Bounds on Sorting

So far, the fastest sorts we have gone over have had $$\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?&#x20;

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)$$is the worst-case runtime of TUCS and give ourselves the challenge of finding the best $$\Theta$$ and $$O$$ bounds for $$R(N)$$. (This might seem a little odd since we really don't know the details of it, but we can find $$R(N)$$).

As a starting point, we can go ahead and have the $$O$$ bound of $$R(N)$$ be $$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.&#x20;

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

![](https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2Fi9dy7konGyPTMThVqL1z%2Fimage.png?alt=media\&token=90743491-8edb-421d-9d25-0c9c61f25cf9)\
\
It turns out we can as if the algorithm sorts all of N elements, it must have gone through all of the N elements at some point of sorting it. Thus, we can arrive at a tighter bound of $$\Omega(N)$$. But can we make it tighter? The answer is yes, but to see the argument for why we have to consider the following game.&#x20;

![](https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2Fgto4SgCjks6DYbeLlekA%2Fimage.png?alt=media\&token=bc3c1bea-f820-4273-a1d0-4e402a5c4866)

### 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.

![](https://lh3.googleusercontent.com/KkM-v6NCPT33GePoBDJ9JgLyKPtfEMPlIU9H0prq3-AX2qTd4_BiKjCw4BCZ82Q1zTeaGPcJK15w2neC5PVAI2EAQjFBdj4bV7qZTz-uPUOg6o9IvyOsSFuu4AfEQFnAAKpc1eU4DIbMjWZUY5ZIMhHA8Q=s2048)

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?&#x20;

The answer turns out to be yes!&#x20;

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2FDEyS11jJNupenGxLSxdu%2Fimage.png?alt=media&#x26;token=26d2f03c-4598-4a23-9e9b-46c783c0072e" alt=""><figcaption></figcaption></figure>

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?

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2F1paSy9ox1eUeTbTCM2e4%2Fimage.png?alt=media&#x26;token=fe3e8632-86db-438d-9862-524d6a59b260" alt=""><figcaption></figcaption></figure>

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?

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2FlFX2h4pTDHicYWpI8Q2F%2Fimage.png?alt=media&#x26;token=276ef08e-3d9f-408b-bd40-5078d3a5fe32" alt=""><figcaption></figcaption></figure>

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.&#x20;

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2Fvs9aIYcZNmMDYmgkmQJI%2Fimage.png?alt=media&#x26;token=f4d8df15-c4aa-4529-8613-a1e4b33a5371" alt=""><figcaption></figcaption></figure>

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).

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2FRH5HtnrifG5oEpu1oNFT%2Fimage.png?alt=media&#x26;token=acddaf20-3ff5-4807-8f51-0b7398008759" alt=""><figcaption></figcaption></figure>

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!$$ ways as there are $$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!)$$levels to reach a leaf. (Note that $$lg$$ means (log base 2)).

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