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

后端 未结 9 484
不思量自难忘°
不思量自难忘° 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:39

    Heapsort would be my reccomendation. It's relatively quick when n is large, and you only have to look at three elements with definite indecies at once.

    That being said, my intuition tells me that sorting a billion rows on an 8080 even in C would be unfeasibly slow.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-01 07:47

    I would not do it in C#, for starters. Are you sure you have this tagged right? This is a C problem, if it can be solved.

    640K only gives you 640 * 1024 * 8 bits so there's no way to solve this as framed. Perhaps that's the answer he/she was looking for. These Investment Bank interviews are something of a mindgame sometimes.

    0 讨论(0)
提交回复
热议问题