## Estimating the n percentile of a set

Here is an interesting idea that I had recently, this is just a high level concept about how it would work, there are no proofs for error bounds or quality, in fact there would be a handful of orderings of sets which would product terrible results.

To accurately compute the percentile value of a given set of values, one ends up having to sort the values which if they are not integers, can take time. However, then getting value itself is trivial since it is simply going to the correct place in the sorted values. I am thinking that one should be able to compute an estimate for the value for a randomly ordered set of elements in time.

First the basic idea, imagine that you have a set of elements . Then if we had this set sorted , then finding the percentile out of would simply become . This implies that we have elements less than and elements greater. From this point we can imagine constructing two sets, which represent the elements greater and less then the value. This also means that . Now using this concept for and , we can attempt to construct these sets while iterating through by having a current estimate for the value , and then tracking the elements current in each set. This essentially becomes if then take the current value of and insert it into , then take the largest element out of and set it equal to . In the case of we simply do the reverse by inserting the current value of into , and then removing the smallest value from and setting it equal to .

Now the problem has been reduce to splitting into two different sets and keeping them sorted somehow to be able to get and remove the largest/smallest elements. However, this would give an exact answer to finding the percentile. Now given that we want to find an estimate, we can imagine capping the size of these sets to , where is a small number such as . Then instead of tracking the elements themselves, we are simply counting the number of elements that are greater or less than the current value. Additionally, we have the sets tracking the elements that are largest but still less then , and the smallest but greater then . As we iterate through the set, we can track the values in and the size of accordingly, and when we want to change the value of to keep , then we just take the new value from or respectively and update the cardinality of each set.

An additional extension to this algorithm could be for an then the size of would only be the size of the original data set. Keeping track of largest of the data set would not be linear in the side of the data set, however it could take out a considerable chunk of computation depending on how large or small the value of is.