Hey. I have a very large array and I want to find the Nth largest value. Trivially I can sort the array and then take the Nth element but I’m only interested in one element so there’s probably a better way than sorting the entire array…
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Sorting would require O(nlogn) runtime at minimum – There are very efficient selection algorithms which can solve your problem in linear time.
Partition-based selection(sometimesQuick select), which is based on the idea of quicksort (recursive partitioning), is a good solution (see link for pseudocode + Another example).