Search code examples
rubyalgorithmsortingtheory

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


Hi I saw that 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 ?


Solution

  • Assuming *nix:

    system("sort <input_file >output_file")
    

    "sort" can use temporary files to work with input files larger than memory. It has switches to tune the amount of main memory and the number of temporary files it will use, if needed.

    If not *nix, or the interviewer frowns because of the sideways answer, then I'll code an external merge sort. See @psyho's answer for a good summary of an external sorting algorithm.