How would you sort something that does not fit in memory?
Anonymous
Assuming "something" is referring to a file, you can do the following: Load a chunk of the file that DOES fit into memory. Sort it in memory, and then write it to a new file. Repeat that process until the entire original file is depleted. You should end up with a bunch of smaller files that are sorted. For example, if the original file was 2GB and you have 500MB of RAM, you would have four 500MB files containing sorted data. Next, take even distributions of those newly sorted files and load them into memory, with an additional buffer of the same size. For instance, since we had four 500MB files in our result set before, we'd load them evenly into memory, taking 125MB from each file. Now do a merge on those 4 125MB buffers and put them into the result buffer. But wait! If we have 4x125MB buffers stored in 500MB RAM, that means we are out of space. So actually, I lied before. We don't want to divide the 500MB RAM into four 125MB buffers, we need an additional buffer to store out result. As such, whatever the amount of files you end up with after your sort, just add one to that number, and divide your RAM by that much. So 500MB RAM / 5 = 5x100MB buffers. Four of the buffers are chunks from the sorted files, and one of the buffers is our result buffer that we'll merge into. Once those buffers are all in memory, run a merge operation on them. If you don't know how to merge sorted lists, look that up separately. When the result buffer is full, write it to a new file, which is the final result file and will eventually be 2GB in size (like the original file) once we've written all the result buffers to it. After you've written the full result buffer to a new result file, empty the buffer so that it has space for a new 100MB of results to be merged into it. While merging the four 100MB buffers into the result buffer, when any of them have been completely merged into the result, grab the next 100MB chunk from their respective sorted file, and continue merging. After all sorted files have been merged, the final result file should contain the 2GB of original data, fully sorted.
Check out your Company Bowl for anonymous work chats.