Find k min balues from array of N elements

Dear Rooters
I’m looking for a way to do the subj faster then first sort the array and then take k elements from the begining. Is there anyhting already implemeted on this in ROOT?

Use TMath::Sort to compute the index of the sorted array (in increasing
or decreasing order), then use this array to loop on the K smallest or biggest
elements. This should be very fast.
You just need to create the index array once. The original array
is not touched.

Rene

Use TMath::KOrdStat and in the index array you’ll have k smallest elements in front, but, unlike the Sort output, they won’t be ordered. It takes only O(n).