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?
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 .
- :
.
, ... , , , . ,
"", , .
30 - . - Omega(n logn), . O(n), , , 30 4 ( ...),
Omega(n logn)
O(n)
. ( ), . , ( , ) . 2, 3 4 . , , ( quicksort now) , , . , O(n logn), n
O(n logn)
n
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.