# 32.2 Quick Select

Dissatisfied with the result that Quicksort is unable to completely defeat Mergesort and truly claim the title for "fastest comparison-based sorting" algorithm, let's shoot our one last shot to overturn the result: use Quick Select to identify the median.&#x20;

## Quick Select Algorithm

Goal: find the median using partioning.&#x20;

Key idea: Median of an length n array will be around index n /2.

1. Initialize array with the leftmost item as the pivot.&#x20;

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2F2pXn0GVshTxyLwc6qOMJ%2FScreen%20Shot%202023-04-10%20at%2012.49.15%20AM.png?alt=media&#x26;token=aaa8c490-cdc8-4426-9fe8-bce26ced7b94" alt=""><figcaption><p>Initial Array</p></figcaption></figure>

For this example, the index of the median should be 9 / 2 = 4.&#x20;

2. Partition around pivot.&#x20;

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2F1st4J7M1QaH2z1l3Usl3%2FScreen%20Shot%202023-04-10%20at%2012.50.44%20AM.png?alt=media&#x26;token=6cede37c-c2b4-4755-b612-118f6e431417" alt=""><figcaption><p>Pivot lands at index 2</p></figcaption></figure>

Since the pivot is at index 2, which is not the median. Therefore, need to continue. However, will only partition the **right** subproblem because median can't be to the left! (index n/2 > 2)

3. Partition the subproblem. Repeat the process.

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2Fm0XHr1ye9N4EVV98EuN5%2FScreen%20Shot%202023-04-10%20at%2012.53.07%20AM.png?alt=media&#x26;token=52877673-f11c-489f-88ab-b0c995e4f377" alt=""><figcaption></figcaption></figure>

Pivot is at index 6, which means that it is not at the median. Continue.&#x20;

4. Stop when the pivot is at the median index.&#x20;

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2FNCuCcZn2wVvMjhLXgRpO%2FScreen%20Shot%202023-04-10%20at%2012.54.52%20AM.png?alt=media&#x26;token=ac35a9ad-d7af-4f1a-a525-20ab9b1bf625" alt=""><figcaption></figcaption></figure>

Since pivot is at index 4, we are done.&#x20;

## Quick Select Performance

### Worst Case Runtime

The worst case is when the array is in sorted order, which yields a runtime of $$\theta(N^2)$$.

![](https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2FYMATfSuOMSDvBWsFU8LR%2FScreen%20Shot%202023-04-10%20at%2012.47.27%20AM.png?alt=media\&token=f2e3f0af-3b98-4395-aacc-e1f33b19fd32)

### Expected Runtime

The expected runtime for the Quick Select Algorithm is $$\theta(N)$$. Here's an intuitive diagram that justifies this result:&#x20;

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2Fo3b6wDKL10J2hWRvqZH5%2FScreen%20Shot%202023-04-10%20at%2012.45.39%20AM.png?alt=media&#x26;token=c11c6df1-2d53-44ac-9a01-07b7c10b7002" alt=""><figcaption></figcaption></figure>

Using one of our favorite sums, we can see the runtime is:

$$
N + N/2 + N/4 +.... + 1 = \theta(N)
$$

## Compared to MergeSort?

Unfortunately, even using Quickselect to find the exact median, the resulting algorithm is still quite slow.&#x20;

Team Mergesort wins, sadly.&#x20;
