The quickest solution is to use the partition-based selection algorithm, which runs in O(n)
. It's based of the quicksort idea, except instead of sorting both partitions recursively, you only go to one of the partitions to find the k-th
smallest element.
Finding the largest 10% is accomplished by searching for the k=(90%*N)-th
smallest number.
If you recall how partitioning in quicksort works, elements less than the pivot is moved to the left, and the rest of the elements go to the right. Let's say you want to select the k-th
smallest element. Then you see if there are at least k
elements to the left of the pivot. If there are, then you know you can ignore the elements in the right partition. Otherwise, you can ignore all elements in the left partition, because you know that element will be in the right partition.
Note that the selection algorithm only identifies what those top 10% numbers are. If you need them to be sorted, then you have to sort those numbers (but only those numbers, the other 90% can be ignored).