What is the best way to sort 30 GB of strings with a computer with 4 GB of RAM using Ruby as a scripting language?

Hi, I saw this as an interview question and thought it was an interesting question that I am not sure about the answer.

What would be the best way?

+5
source share
5 answers

Assuming * nix:

system("sort <input_file >output_file")

"sort" can use temporary files to work with input files larger than memory. It has switches for adjusting the amount of main memory and the number of temporary files that will be used if necessary.

* nix, - , . . @psyho .

+8

- :

  • , (, quicksort)
  • 1-3 ,
  • Profit!
+5

.

+4

, ... , , , . ,

"", , .

1

30 - . - Omega(n logn), . O(n), , , 30 4 ( ...),

2 ()

. ( ), . , ( , ) . 2, 3 4 . , , ( quicksort now) , , . , O(n logn), n

+3

.

A good answer is to use the merge sort algorithm, adapting it to buffer data to and from disk, as necessary for the merge steps. This can be done with minimal memory requirements.

+2
source