The Art of Writing Short Stories Read Here

Sorting larger file with smaller RAM

 Suppose we have to sort a 1GB file of random integers and the available ram size is 200 Mb, how will it be done?

The easiest way to do this is to use external sorting.
We divide our source file into temporary files of size equal to the size of the RAM and first sort these files.
Assume 1GB = 1024MB, so we follow following steps.

  1. Divide the source file into 5 small temporary files each of size 200MB (i.e., equal to the size of ram).
  2. Sort these temporary files one bye one using the ram individually (Any sorting algorithm : quick sort, merge sort).

Now we have small sorted temporary files as shown in the image below.

Figure – Dividing source file in smaller sorted temp files

Now we have sorted temporary files.

  1. Pointers are initialized in each file
  2. A new file of size 1GB (size of source file) is created.
  3. First element is compared from each file with the pointer.
  4. Smallest element is copied into the new 1GB file and pointer gets incremented in the file which pointed to this smallest element.
  5. Same process is followed till all pointers have traversed their respective files.
  6. When all the pointers have traversed, we have a new file which has 1GB of sorted integers.
You may also like :