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.
- Divide the source file into 5 small temporary files each of size 200MB (i.e., equal to the size of ram).
- 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.
Now we have sorted temporary files.
- Pointers are initialized in each file
- A new file of size 1GB (size of source file) is created.
- First element is compared from each file with the pointer.
- Smallest element is copied into the new 1GB file and pointer gets incremented in the file which pointed to this smallest element.
- Same process is followed till all pointers have traversed their respective files.
- When all the pointers have traversed, we have a new file which has 1GB of sorted integers.
This is how any larger file can be sorted when there is a limitation on the size of primary memory (RAM).
The basic idea is to divide the larger file into smaller temporary files, sort the temporary files and then creating a new file using these temporary files. This question was asked in Infosys interview for power programmer profile.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Different Types of RAM (Random Access Memory )
- Random Access Memory (RAM) and Read Only Memory (ROM)
- Difference between RAM and ROM
- Difference between Random Access Memory (RAM) and Hard Disk Drive (HDD)
- Difference between Random Access Memory (RAM) and Content Addressable Memory (CAM)
- Magnetic Random Access Memory (M-RAM)
- RAM Full Form
- Difference between RAM and SRAM
- Difference between RAM and Cache
- 8085 program to find larger of two 8 bit numbers
- Difference between Local File System (LFS) and Distributed File System (DFS)
- File Allocation Methods
- File Systems in Operating System
- Unix File System
- Path Name in File Directory
- File System Consistency Checker (FSCK)
- Log-Structured File System (LFS)
- Network File System (NFS)
- SetUID, SetGID, and Sticky Bits in Linux File Permissions
- Various terms in File System
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.