Fastest way to obtain the largest X numbers from a very large unsorted list?

后端 未结 3 793
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-15 04:51

I\'m trying to obtain the top say, 100 scores from a list of scores being generated by my program. Unfortuatly the list is huge (on the order of millions to billions) so sorting

3条回答
  •  余生分开走
    2021-02-15 05:51

    1. take the first 100 scores, and sort them in an array.
    2. take the next score, and insertion-sort it into the array (starting at the "small" end)
    3. drop the 101st value
    4. continue with the next value, at 2, until done

    Over time, the list will resemble the 100 largest value more and more, so more often, you find that the insertion sort immediately aborts, finding that the new value is smaller than the smallest value of the candidates for the top 100.

提交回复
热议问题