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

后端 未结 30 2039
栀梦
栀梦 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 14:43

    We have 1 MB - 3 KB RAM = 2^23 - 3*2^13 bits = 8388608 - 24576 = 8364032 bits available.

    We are given 10^6 numbers in a 10^8 range. This gives an average gap of ~100 < 2^7 = 128

    Let's first consider the simpler problem of fairly evenly spaced numbers when all gaps are < 128. This is easy. Just store the first number and the 7-bit gaps:

    (27 bits) + 10^6 7-bit gap numbers = 7000027 bits required

    Note repeated numbers have gaps of 0.

    But what if we have gaps larger than 127?

    OK, let's say a gap size < 127 is represented directly, but a gap size of 127 is followed by a continuous 8-bit encoding for the actual gap length:

     10xxxxxx xxxxxxxx                       = 127 .. 16,383
     110xxxxx xxxxxxxx xxxxxxxx              = 16384 .. 2,097,151
    

    etc.

    Note this number representation describes its own length so we know when the next gap number starts.

    With just small gaps < 127, this still requires 7000027 bits.

    There can be up to (10^8)/(2^7) = 781250 23-bit gap number, requiring an extra 16*781,250 = 12,500,000 bits which is too much. We need a more compact and slowly increasing representation of gaps.

    The average gap size is 100 so if we reorder them as [100, 99, 101, 98, 102, ..., 2, 198, 1, 199, 0, 200, 201, 202, ...] and index this with a dense binary Fibonacci base encoding with no pairs of zeros (for example, 11011=8+5+2+1=16) with numbers delimited by '00' then I think we can keep the gap representation short enough, but it needs more analysis.

提交回复
热议问题