Top n items in a List ( including duplicates )

前端 未结 5 2058
谎友^
谎友^ 2021-01-20 22:18

Trying to find an efficient way to obtain the top N items in a very large list, possibly containing duplicates.

I first tried sorting & slicing, which works. But

5条回答
  •  难免孤独
    2021-01-20 22:26

    The classic algorithm is called QuickSelect. It is like QuickSort, except you only descend into half of the tree, so it ends up being O(n) on average.

提交回复
热议问题