# Sort Big File

**10.6 Sort Big File:**Imagine you have a 20 GB file with one string per line. Explain how you would sort the file.

- Thoughts when I have no idea:
- Cant fit in RAM
- Assuming 4 GB of RAM, would take in 4GB of data at a time, and then group the data into 5 different containers of disk. The amount of containers that I will need will be = ceil(26/5), where Container 1 contains words that start with {a,b,c,d,e}, Container 2 = {f,g,h,i,j}, ..., and Container 6 = {w,x,y,z}.
- Quick sort all containers (in-place alg, no extra memory used).
- Combine containers by on disk sequentially in their preserved order.

- Solution:
- Bring in X amount of data into X amount of memory and sort each chuck in the file. Then preform inplace_merge on each check until the entire array is sorted.
- For example, if you have 8 pages, and your memory can take in 2 pages at a time. Then sort the first sequential 2, until you reach the end of the pages.
- Then sort each sequential 4 by merging the two together.
- Finally sort each sequential 8 by merging the 4 together.
- To understand why we can merge more than 4 pages at a time we have to know how inplace merge works. In place merge compares at most two elements together (2 pages). Since the elements are sorted, we just have to look at the top of the stack of the two stacks. We select the pages one by one and place them into disk at the same time in a sorted manner.

Last modified 3yr ago