UPI Study › Courses › Data Structures and Algorithms › Free lessons › Lesson 9.4 Free with a free account
Doubling the Search Range Until the Target Is Found
Data Structures and Algorithms · Chapter 9: SearchingSection 9.4 · lesson 38 of 8710 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.
Next lesson: Stacks →← Splitting a Sorted Collection in Half, RecursivelyNext lesson: Stacks →
In this lesson
Exponential search doubles a bound, then binary searches the bracket.
- Explain how exponential search doubles a bound to bracket the target
- Implement exponential search with a capped bracket and a binary search phase
- Search sorted data of unknown size using a sentinel past the end
- Compare the order log i cost of galloping with the order log n cost of binary search
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,520 words). Read along, or search it.
Opening Question
A Directory of Unknown Length
You're looking for a name in a printed directory, but you don't know how many pages it has. You can't open it in the middle, because you can't say where the middle is. So you look at page one, then two, then four, then eight, doubling each time, until you land on a name that comes after yours. Now your name lies between the last two pages you looked at, and you search only that stretch. The trick helps even when you do know the size. If your name is near the front, there's no sense starting in the middle of a huge book. This is exponential search, also called galloping search.
The Problem
Two Weak Spots of Binary Search
Plain binary search has two weak spots. When the size is unknown, there's no last position, so there's no middle to probe. Think of a sorted feed read one item at a time, or a quantity that keeps growing. And when the target is near the front of a huge array, the first probe still lands in the middle. It still needs about twenty probes for a million values, even if the target sits at position three. One cure fixes both: find a small range that must hold the target, at a cost that depends on how far from the front it is. Then binary search that range.
Phase One
Doubling the Bound to Trap the Target
Phase one doubles a bound. Take the twenty even numbers from two to forty, sorted, and search for twenty-two. Position zero holds two, not the target. So set the bound to one, and while the value at the bound is smaller than the target, double it. Position one holds four: smaller. Position two holds six, position four holds ten, and position eight holds eighteen: all smaller. Position sixteen holds thirty-four. That's not smaller, so the gallop stops. Why is that enough? The previous bound, eight, held a smaller value, so the target isn't at or before it. The current bound holds a larger value, so it isn't after it. The target is trapped in a bracket, from half the bound to the bound. Here, that's positions eight to sixteen.
Phase Two
Binary Search Inside the Bracket
Phase two is an ordinary binary search inside the bracket. The middle of eight and sixteen is twelve, holding twenty-six: too big. Then position nine holds twenty: too small. Position ten holds twenty-two. Found. One read at position zero, five gallop reads, and three binary probes: nine reads in all.
Implementation
Read Helper, Gallop, and Capped Bracket
In code, a helper called read returns the value at a position, and counts each read. Past the end, it returns the largest possible int, a detail that matters soon. The method returns minus one for an empty array, and zero if position zero matches. Then the gallop: while the bound is inside the array, and the value there is too small, double it. The bracket runs from half the bound to the bound, capped at the last position, because the final doubling may overshoot the end. And binary search range is the binary search from before, limited to positions low through high.
Worked Example
Reads for Four Different Targets
Run it on the same twenty values. The target two is at position zero, and costs one read. Eight costs five reads: position zero, the bounds one, two and four, then one binary probe at position three. Thirty-six shows the cap. The bound doubles from sixteen to thirty-two, past the end, so the bracket is cut to positions sixteen through nineteen. Seven reads. And a hundred is larger than everything. The search reaches the end, and correctly reports minus one, after nine reads. If the target is smaller than everything, the bracket is just positions zero to one. And with duplicates, it returns one matching position, not necessarily the first.
Unknown Size
A Sentinel Past the End
Now remove one assumption. Suppose the program never learns how many values there are. It can read by position, but the length is never revealed. Plain binary search can't even begin. Exponential search can, because phase one never asks for the size. It only needs to learn that a probe has gone past the end. The trick is a sentinel: a read beyond the real data returns a value larger than anything that could be searched for. The read helper already does that, with the largest int. So positions past the end act like infinity. A probe that overshoots just looks too big, and ends the gallop. The top of the bracket is simply the bound, with no cap. A test hides a hundred thousand values, one, four, seven, ten, and so on, from the search, which is never told the length. The value at position forty costs thirteen reads. The one at position seventy thousand costs thirty-five. A missing value like two returns minus one. So does a value larger than everything: the first infinity ends the gallop. Two cautions. The data must really be sorted. And the sentinel must be larger than every value that might be searched for, including the target itself.
Quick Check
The Bracket for Sixteen
Quick check. Six sorted values, one, four, seven, ten, thirteen, sixteen, are hidden behind the unknown-size search. Search for sixteen. Which bracket does phase one hand to the binary search? Four to eight, two to four, or four to five? Pause, and predict. Four to eight. The gallop reads positions one, two and four, holding four, seven and thirteen: all smaller than sixteen. Position eight is past the end, so it reads as the largest int. That's not smaller, and the gallop stops. The method never knows the length, so the top isn't cut to five. Inside the bracket, position six also reads as the largest int, and the search still finds sixteen at position five.
Analysis
About Two Log i Reads
Now count the work. Let i be the target's position, counting from zero. In phase one, the bound takes the values one, two, four, eight, and stops near i. That takes about log two of i doublings. In phase two, the bracket runs from half the bound to the bound. The bound is under two i, so the bracket is shorter than i, and halving it takes about log i probes. Together, that's about two log i reads. Big O drops the two, so the cost is order log i. Plain binary search costs order log n, where n is the size of the whole array. Exponential search depends on how far the target is from the front.
Experiment
Break-Even Near the Square Root of n
Measured on a million sorted values: at position ten, the gallop uses nine reads, against eighteen for binary search. At position one hundred, it's thirteen against twenty. The two meet near position one thousand, the square root of a million. That's no accident. Two log i is smaller than log n, exactly when i is smaller than the square root of n. Beyond that, the gallop loses. At position ten thousand, it needs twenty-nine reads against seventeen, and at the very end, forty against twenty: about twice as many. But since i can never exceed n, it's never worse by more than a constant factor.
Quick Check
Break-Even for Two to the Sixteenth
One more check. A sorted array holds sixty-five thousand, five hundred thirty-six values: two to the sixteenth. Roughly where do the gallop and binary search break even? Position two fifty-six, four thousand ninety-six, or thirty-two thousand? Pause, and decide. Around position two fifty-six, the square root of n. Binary search needs about sixteen reads anywhere, since log n is sixteen. At two fifty-six, the gallop needs about two times eight: sixteen as well. At four thousand ninety-six, it needs about twenty-four, already worse, and at thirty-two thousand, about thirty.
Decision Guide
Gallop or Plain Binary Search
So which should you use? If the array is sorted, its size is known, and the target could be anywhere, use plain binary search. Galloping gains nothing there, and may cost twice as much. If the length is never given, use exponential search, since binary search can't start. And if most lookups hit the first few hundred positions, gallop too. For unsorted data, neither works: scan from the front, or sort first. And for twenty values, either is fine, so pick the simpler plain binary search.
Common Mistakes
Four Galloping Pitfalls
Watch for four mistakes. Start the bound at zero, and it never moves: zero doubled is still zero. Forget to cap the top of the bracket, and the search reads past the end of the array. Start phase two at position zero, and it stays correct, but throws the savings away. Begin at half the bound. And for arrays of more than about a billion positions, doubling an int bound can overflow. Use a long.
Key Takeaways
Exponential Search Essentials
To sum up. Exponential search doubles a bound, one, two, four, eight, until the value there reaches the target. Then it binary searches the bracket, from half the bound to the bound. It works on data of unknown size, when positions past the end read as a sentinel larger than any target. It costs order log i, which beats binary search roughly when i is below the square root of n.
Think About It
Word Suggestions from a Sorted List
Something to think about. A dictionary app suggests words as you type, from a sorted list of a million words. Most lookups start with common prefixes near the front. Would you gallop, or use plain binary search? What would you measure to decide?
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 Doubling the Search Range Until the Target Is Found
What is exponential search?
What is the time complexity of exponential search?
How do you search a sorted list of unknown size?
When should you use exponential search instead of binary search?
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 →