Sorting 1 million 8-decimal-digit numbers with 1 MB of RAM

后端 未结 30 2071
栀梦
栀梦 2020-12-22 14:33

I have a computer with 1 MB of RAM and no other local storage. I must use it to accept 1 million 8-digit decimal numbers over a TCP connection, sort them, and then send the

30条回答
  •  鱼传尺愫
    2020-12-22 15:04

    Suppose this task is possible. Just prior to output, there will be an in-memory representation of the million sorted numbers. How many different such representations are there? Since there may be repeated numbers we can't use nCr (choose), but there is an operation called multichoose that works on multisets.

    • There are 2.2e2436455 ways to choose a million numbers in range 0..99,999,999.
    • That requires 8,093,730 bits to represent every possible combination, or 1,011,717 bytes.

    So theoretically it may be possible, if you can come up with a sane (enough) representation of the sorted list of numbers. For example, an insane representation might require a 10MB lookup table or thousands of lines of code.

    However, if "1M RAM" means one million bytes, then clearly there is not enough space. The fact that 5% more memory makes it theoretically possible suggests to me that the representation will have to be VERY efficient and probably not sane.

提交回复
热议问题