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

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

    Gilmanov's answer is very wrong in its assumptions. It starts speculating based in a pointless measure of a million consecutive integers. That means no gaps. Those random gaps, however small, really makes it a poor idea.

    Try it yourself. Get 1 million random 27 bit integers, sort them, compress with 7-Zip, xz, whatever LZMA you want. The result is over 1.5 MB. The premise on top is compression of sequential numbers. Even delta encoding of that is over 1.1 MB. And never mind this is using over 100 MB of RAM for compression. So even the compressed integers don't fit the problem and never mind run time RAM usage.

    It's saddens me how people just upvote pretty graphics and rationalization.

    #include 
    #include 
    #include 
    
    int32_t ints[1000000]; // Random 27-bit integers
    
    int cmpi32(const void *a, const void *b) {
        return ( *(int32_t *)a - *(int32_t *)b );
    }
    
    int main() {
        int32_t *pi = ints; // Pointer to input ints (REPLACE W/ read from net)
    
        // Fill pseudo-random integers of 27 bits
        srand(time(NULL));
        for (int i = 0; i < 1000000; i++)
            ints[i] = rand() & ((1<<27) - 1); // Random 32 bits masked to 27 bits
    
        qsort(ints, 1000000, sizeof (ints[0]), cmpi32); // Sort 1000000 int32s
    
        // Now delta encode, optional, store differences to previous int
        for (int i = 1, prev = ints[0]; i < 1000000; i++) {
            ints[i] -= prev;
            prev    += ints[i];
        }
    
        FILE *f = fopen("ints.bin", "w");
        fwrite(ints, 4, 1000000, f);
        fclose(f);
        exit(0);
    
    }
    

    Now compress ints.bin with LZMA...

    $ xz -f --keep ints.bin       # 100 MB RAM
    $ 7z a ints.bin.7z ints.bin   # 130 MB RAM
    $ ls -lh ints.bin*
        3.8M ints.bin
        1.1M ints.bin.7z
        1.2M ints.bin.xz
    

提交回复
热议问题