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

后端 未结 30 2127
栀梦
栀梦 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:07

    I would exploit the retransmission behaviour of TCP.

    1. Make the TCP component create a large receive window.
    2. Receive some amount of packets without sending an ACK for them.
      • Process those in passes creating some (prefix) compressed data structure
      • Send duplicate ack for last packet that is not needed anymore/wait for retransmission timeout
      • Goto 2
    3. All packets were accepted

    This assumes some kind of benefit of buckets or multiple passes.

    Probably by sorting the batches/buckets and merging them. -> radix trees

    Use this technique to accept and sort the first 80% then read the last 20%, verify that the last 20% do not contain numbers that would land in the first 20% of the lowest numbers. Then send the 20% lowest numbers, remove from memory, accept the remaining 20% of new numbers and merge.**

提交回复
热议问题