How to sort millions of rows of data in a file with less/meagre memory

后端 未结 9 525
不思量自难忘°
不思量自难忘° 2021-02-01 06:44

(From here)

I attended an interview last week and this question was asked:

How do you sort a billion rows of data in a file with only 640KB of memory in

9条回答
  •  梦谈多话
    2021-02-01 07:40

    The more I think about this, the more I think merge sort would work very well within the memory window we're given.

    Let's say you have x memory available. Divide the billion entries into billion/x + 1 sections and heapsort them (heapsort because no extra memory is required and it's O(2n(log n)) time). When all sections are heapsorted, do a merge sort starting across the first elements of all sections. This will work so long as you have more than sqrt(billion) memory to work with given basic 8080 OS memory usage.

    Doing the math, this assumes that each data row is less than 165 bits.

提交回复
热议问题