# Quick select

Posted: | More posts about mathjax

Why would a programmer want to write their own sort function?

First, and most importantly, to learn about algorithms and programming. Also, a language or library sometimes has an inadequate sort function for a specific situation.

There is also a problem in the wild that a custom sort is nice to use for.

Finding the median of an array of data (or any other percentile, for that matter.) Quickselect is closely related to Quicksort. Instead of recursively sorting both halves of the partition created in the partitioning step, we can just follow the one that contains the element we are looking for.

The C++ STL contains a partition function that can do just what we want. Sometimes a language / library doesn't include this functionality.

Quickselect is an \(O(n)\) algorithm in the average case, which sounds much better than the \(O(n \log n)\) case for Quicksort. But, once \(n\) gets large enough, these two grow at about the same rate. The bigger win is that Quickselect has a lower constant, so even though both it and Quicksort grow at about the same rate, Quickselect is a few times faster on random data.

So, here is a `quickMedian` function. This function keeps calling a
partitioning function on the subarray that contains the middle point.
(Not really the median … but it is our example.)

The partition function returns and object containing several indexes.

- a—the start of the left partition
- b—the end of the left partition
- c—the start of the right partition
- d—the end of the right partition

We also jump out early instead of breaking the array down to a single element, and use insertion sort on the last chunk of the array.

1 function quickMedian(xs) { 2 var N = xs.length, 3 start = 0, end = N, 4 middle = Math.floor(N/2), 5 idxs; 6 7 while(end - start > INSERT_SORT_THRESHOLD) { 8 idxs = qsort2Partition(xs, start, end); 9 10 if(idxs.b > middle) { 11 // search left 12 end = idxs.b; 13 } else if (idxs.c < middle) { 14 start = idxs.c; 15 } else { 16 // We have contained the element! 17 break; 18 } 19 } 20 21 isort(xs, start, end); 22 if(middle < start || middle > end) { 23 throw 'bug... partitioning failed'; 24 } 25 return xs[middle]; 26 }

This function can be generalized to find a different value than the center value, or even to find several percentiles at once.

Here are some non-representative timing results, from my Mac, on Chrome.

data type | quick sort | quick select median |
---|---|---|

random double | 0.15 | 0.02 |

sawtooth [1] | 0.02 | 0.02 |

ordered | 0.04 | 0.02 |

reversed | 0.05 | 0.02 |

shuffled | 0.15 | 0.03 |

[1] | The values in the array are 0, 1, 2, 3, 0, 1, 2, 3, … the whole way to the end of the array. |

Here is a link to the test driver that launches directly into the tests. It runs in Chrome, but hasn't been tested extensively.