Find top N elements in an Array

前端 未结 12 1235
南笙
南笙 2020-11-28 06:18

What would be the best solution to find top N (say 10) elements in an unordered list (of say 100).

The solution which came in my head was to 1. sort it using quick s

12条回答
  •  感情败类
    2020-11-28 06:59

    How about delegating everything to Java ;)

    function findTopN(Array list, int n)
    {
        Set sortedSet = new TreeSet<>(Comparators.naturalOrder());
    
        // add all elements from list to sortedSet
    
        // return the first n from sortedSet
    }
    

    I am not trying to say that this is the best way. I still think Yin Zhu's method of finding the kth largest element is the best answer.

提交回复
热议问题