Retrieving the top 100 numbers from one hundred million of numbers

前端 未结 12 2151
北荒
北荒 2020-11-30 19:36

One of my friend has been asked with a question

Retrieving the max top 100 numbers from one hundred million of numbers

in a rece

12条回答
  •  眼角桃花
    2020-11-30 19:51

    First iteration:

    Quicksort, take top 100. O(n log n). Simple, easy to code. Very obvious.

    Better? We are working with numbers, do a radix sort (linear time) take the top 100. I would expect this is what the interviewer is looking for.

    Any other considerations? Well, a million numbers isn't a lot of memory, but if you want to minimize memory, you keep a max 100 numbers encountered so far and then just scan the numbers. What would be the a best way?

    Some have mentioned a heap, but a bit better solution might be a doubly-linked list, where you keep the pointer to the minimum of the top 100 found so far. If you encounter a number a that is bigger than the current smallest in the listed, compared with the next element, and move the number from next to the current until you find a place for the new number. (This is basically just a specialized heap for the situation). With some tuning (if the number is greater the current minimum, compare with current maximum to see which direction to walk list to find insertion point) this would be relatively effective, and would only take like 1.5k of memory.

提交回复
热议问题