UPI Study › Courses › Data Structures and Algorithms › Free lessons › Lesson 17.10 Free with a free account
Arranging Data Too Large to Fit in Main Memory
Data Structures and Algorithms · Chapter 17: Graphs and MemorySection 17.10 · lesson 87 of 878 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Data Structures and Algorithms.
Lesson complete
Earn the 3 college credits for Data Structures and Algorithms
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
← Navigating Massive Disk Datasets with Multi-Level Index Trees
In this lesson
External merge sort, replacement selection, and choosing fan-in and block size.
- Count the block transfers of an external merge sort
- Compute the number of merge passes from the number of runs and the fan-in
- Trace replacement selection and explain why it produces longer runs
- Choose a fan-in and block size that balance seeks against passes
Free study notes
Get this lesson's notes by email
The key points and 4 practice questions with answers, in your inbox. One email, free.
One email. Unsubscribe in one click.
Transcript
Everything said in the video, section by section (1,097 words). Read along, or search it.
Opening Question
Sorting a File You Cannot Load
Memory holds 10,000 records. The file holds a million, a hundred times more. Sort it. The data lives on a disk, which is thousands of times slower than memory, and hands over data in blocks, not one value at a time. So what's the cheapest way to sort a file you can't even load?
Cost Model
Block Transfers and Two Phases
Call the file N records, memory M records, and a block B records. The cost is the number of block transfers: each block read from disk, or written to it. On a spinning disk, every transfer pays a seek of several milliseconds. At 8 milliseconds, that's 8 million nanoseconds: the time of millions of comparisons in memory. That points to merge sort. Merging reads each input from start to end, and writes its output the same way, exactly what a disk does well. So external merge sort has two phases. Run formation cuts the file into sorted runs, as long as memory allows. Then merging combines k runs at a time, until one run is left.
Run Formation
Load, Sort, and Write Sorted Runs
This program counts every block it moves. The disk is simulated with arrays, but the counting follows the same rule: one transfer per block. Run formation is the simplest step. Load M records, sort them with any in-memory sort, and write them out as one run. Repeat until the input ends. Run it on a million records, with memory for 10,000 and blocks of 1,000. That's 100 runs, and each of the 1,000 blocks is read once and written once.
Multiway Merge
Merging Nine Runs at a Time
Merging two runs means comparing their front values and writing the smaller. A k-way merge does the same with k runs at once, using a heap of their front values. Take the smallest, write it, and refill the heap from that same run. Memory holds one block from each input run, plus one output block. So the fan-in is limited to M over B, minus one. Here that's 10 blocks of memory, so 9 runs at a time. The passes cut 100 runs to 12, then 2, then 1. Three merge passes, 8,000 transfers in all, and the output is sorted correctly.
Formula
Predicting the Transfer Count
That count can be written down in advance. Run formation reads and writes every block once. So does each merge pass. With fan-in k, each pass divides the number of runs by k. So the passes needed are the smallest p with k to the p at least R, the number of runs. Here, 9 squared is 81, less than 100, so p is 3. The total is 2 times N over B, times 1 plus p: 2 times 1,000, times 4. Exactly 8,000.
Quick Check
Merge Passes for Two Hundred Runs
Quick check. A sort forms 200 runs, and memory allows a fan-in of 6. How many merge passes does it need? Pause and predict. Three. Six squared is only 36 runs' worth, short of 200. Six cubed is 216, which covers them. Thirty-four would be 200 divided by 6: that's the number of runs after the first pass, not the number of passes.
Replacement Selection
Runs Longer Than Memory
Fewer runs can mean fewer passes. Replacement selection makes runs longer than memory, using a min-heap of M records. Repeatedly, remove the smallest record, write it to the current run, and read the next input record. If it's at least as large as the last one written, it joins this run. If it's smaller, it's tagged for the next run, and waits in the heap behind everything in the current one. Run it on fourteen values with room for only three. The runs come out with lengths 4, 6, and 4: longer than memory, because large enough records pass straight through.
Measurement
Fifty-One Runs Instead of a Hundred
How much longer, in practice? This program forms runs from a million records, with memory for 10,000. Load-sort-write always makes 100 runs. On random input, replacement selection made 51: runs about twice as long as memory, as a classic snowplow argument predicts. Ascending input becomes a single run, with no merging at all. Descending input gains nothing: every record waits for the next run. In our sort, 51 runs with a fan-in of 9 need only two passes. That's about 6,000 transfers instead of 8,000, a quarter fewer.
Quick Check
The First Run With Room for Two
Quick check. Replacement selection, with room for just two records, reads 5, 3, 8, 1, 9, 2, and 7. What is the first run? Pause and predict. Three, five, eight, nine. Three goes out first, and 8 is large enough to join. Then 5 goes out, but 1 is too small, so it waits. Eight goes out, and 9 joins. Nine goes out, and 2 waits too. The first run is twice as long as memory, and the second is 1, 2, 7.
Fan-In
Where the Pass Count Goes Flat
How much does the fan-in matter? This run sorts the million records as 100 runs, with blocks of 100 records, so any fan-in up to 99 fits. A binary merge needs 7 passes and 160,000 transfers. A fan-in of 16 needs only 2 passes: 60,000 transfers, about 2.7 times less. Then the curve goes flat. Sixteen squared is already 256, more than 100 runs, so fan-ins of 32 or 99 still need 2 passes. Memory spent on a bigger fan-in beyond that threshold buys nothing.
Block Size
Balancing Seeks Against Passes
Block size pulls two ways. Bigger blocks mean fewer transfers, but a smaller fan-in, which can add passes. This program models a disk: every transfer pays an 8 millisecond seek, then moves its block at 100 megabytes per second, with 100-byte records. It's a model, not a measured disk. Blocks of 100 need just 2 passes, but 60,000 seeks: 486 modeled seconds. Blocks of 2,000 need 4 passes, yet only 5,000 transfers: 50 seconds. At 2,500, the fan-in drops to 3, five passes are needed, and the time creeps back up. The best block size sits in the middle.
Decision Guide
Spending Memory Wisely
So the choices come down to memory. Use all of it to form runs, and prefer replacement selection when the input is random, or partly in order. Make the fan-in just large enough to reach the pass count you need, and always keep one block for output. Spend leftover memory on larger blocks, and test candidate block sizes with the formula. Count passes first: every pass costs two transfers per block of the file.
Key Takeaways
External Sorting in Brief
To sum up. When data won't fit in memory, cost is counted in block transfers. External merge sort forms sorted runs, then merges k at a time, with k equal to M over B, minus one. The total is two times N over B, times one plus the number of passes. Replacement selection doubles run length on random input, and the best block size balances seeks against passes.
About Data Structures and Algorithms at UPI Study
Data Structures and Algorithms (Computer Science 251) is a self-paced online course of 87 video lessons in 17 chapters, from Java and object-oriented design to arrays, linked lists, recursion, stacks and queues, trees, heaps, hash tables, sorting and graphs. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Compare: Discrete Math vs Data Structures and Algorithms · Intro to Java vs Data Structures and Algorithms
Why take it here
Why students take Data Structures and Algorithms with UPI Study
Where the credit goes
Universities that work with NCCRS
Data Structures and Algorithms carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Arranging Data Too Large to Fit in Main Memory
How does external merge sort work?
How many passes does an external merge sort need?
What is replacement selection in external sorting?
How does block size affect external sorting?
About the Data Structures and Algorithms course
How many college credits is Data Structures and Algorithms?
How much does Data Structures and Algorithms cost and what is included?
Which universities accept UPI Study credits?
How long does Data Structures and Algorithms take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in Data Structures and Algorithms.
87 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
3 credits · NCCRSEnroll →