Lecture notes · Programming in Python · Lecture 6

Bisection Search

MIT 6.100L Intro to CS and Programming using Python, lecture 6, explained in plain English

Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.

Learn this lecture free

Watch or read the original lecture: MIT OpenCourseWare.

Part 1 · 0:00–10:00

This part reviews why simple guessing is slow, shows why approximation needs a small margin of error, and sets up bisection search as a faster method.

Key ideas

Words to know

epsilon
A small wiggle-room number. An answer counts as close enough if it is within plus or minus epsilon of the target.
floating point number
A number with a decimal point, such as 0.1. Computers store these with tiny rounding, so they are not always exact.
infinite loop
A loop that never stops because its stopping condition is never met.
guess and check
A method that tries possible answers one by one until it finds one that works. It is slow when there are many possible answers.

Example

Ana's book challenge asks you to find a $100 bill somewhere in a 448-page book in eight or fewer guesses. Checking pages one at a time could take hundreds of tries, so the lecture shows why a smarter way of guessing is needed.

3 checked practice questions for this part are in the app.

Part 2 · 10:00–20:00

Bisection search is a fast way to find an answer in an ordered list by guessing the middle and throwing away the half that cannot hold the answer.

Key ideas

Words to know

bisection search
A method that finds an answer in an ordered list by checking the middle, then keeping only the half that could contain the answer, and repeating.
interval
The range between a low end and a high end where the answer must be. In the book example, the interval started as pages 1 to 448.
logarithmic growth
When the number of steps grows very slowly as the problem gets bigger, because the search space is cut in half at each stage.
linear growth
When the number of steps grows in step with the size of the problem, such as checking one page at a time.

Example

Imagine finding one person in a class lined up alphabetically by last name. You ask the person in the middle for their last name, then ignore the half of the line that cannot contain your person. You repeat this until only one person is left, and that person is the one you want or is not there.

3 checked practice questions for this part are in the app.

Part 3 · 20:00–30:00

This part explains bisection search, a faster way to find a square root by repeatedly guessing the middle of a range and cutting the range in half.

Key ideas

Words to know

bisection search
A way of finding an answer by guessing the middle of a range, then keeping only the half that could hold the answer, and repeating.
midpoint
The point exactly halfway between two numbers. The lesson uses the average of low and high, which is (high + low) divided by 2.
epsilon
A tiny allowed margin of error. Being within plus or minus epsilon means close enough to count as the answer.
feedback
The clue you get back after each guess, such as whether the guess was too high, too low, or correct.

Example

Think of a number guessing game where a friend picks a number from 1 to 100 and says higher or lower after each guess. If you guess 50 and hear higher, you know the answer is between 51 and 100, so you ignore everything from 1 to 50. You keep guessing the middle of what is left, and the range shrinks fast.

3 checked practice questions for this part are in the app.

Part 4 · 30:00–40:00

This part shows how bisection search finds a square root by cutting the search range in half on every guess, and why that is far faster than trying numbers one at a time.

Key ideas

Words to know

bisection search
A way of finding an answer by guessing the middle of a range, then keeping only the half that still holds the answer.
epsilon
The small amount of error you accept. If your answer is within epsilon, you call it close enough.
logarithmic growth
When the work needed grows very slowly compared with the size of the problem. Each extra step covers twice as much ground.
while loop
A block of code that keeps repeating as long as a condition is still true, and stops when it becomes false.

Example

Imagine finding one page in a 400-page book by opening to the middle and then always going to the half that still has the page. After one check you have 200 pages left, then 100, then 50. Bisection search works the same way with numbers.

3 checked practice questions for this part are in the app.

Part 5 · 40:00–50:00

This part explains why a bisection search program got stuck in an endless loop, how to fix its starting range, and why bisection search is so fast.

Key ideas

Words to know

infinite loop
A loop that never stops because its stopping condition is never reached, so the program keeps repeating the same steps.
floating point error
A tiny rounding mistake that happens because computers cannot store every decimal number exactly.
bisection search
A way of finding an answer by repeatedly guessing the middle of a range and throwing away the half that cannot hold the answer.
logarithmic growth
A pattern where each step makes a smaller and smaller improvement, so the results level off over time.

Example

Imagine finding a page in a 1,000-page book by opening it to the middle. If the page is too far ahead, you throw away the back half and repeat with the front half. The lesson's book example works this way: the first guess removed 200 pages, the next removed 100, then 50, and so on, so the search gets very close to the answer quickly.

3 checked practice questions for this part are in the app.

Part 6 · 50:00–1:00:00

This part shows how to write a bisection search that finds a cube root, and introduces Newton-Raphson, another way to get closer to a root.

Key ideas

Words to know

bisection search
A way of finding an answer by repeatedly guessing the middle of a range and cutting the range in half, based on whether the guess is too high or too low.
epsilon
A small margin of error. The answer counts as good enough when it is within this distance of the target.
polynomial
A math expression made of powers of x added together, such as x squared minus 24.
derivative
A formula that shows how fast a polynomial is changing at a given point. Newton-Raphson needs it to pick a better guess.

Example

Imagine guessing a secret number between 1 and 100, and a friend tells you 'higher' or 'lower' after each guess. If you always guess the middle of the range that is still possible, you find the number quickly. This matches bisection search: pick the middle, use the feedback, and cut the range in half.

3 checked practice questions for this part are in the app.

Part 7 · 1:00:00–1:10:00

This part shows how a faster guessing method, Newton-Raphson, finds square roots, and previews the big ideas of decomposition and abstraction.

Key ideas

Words to know

derivative
A number that tells you how steeply a line or curve is going up or down at one specific point.
epsilon
A small margin of error. You accept an answer if it is within this distance of the right answer.
decomposition
Breaking one big job into smaller separate pieces that each handle one part of the job.
abstraction
Hiding the messy details of how something works, so you only need to know what goes in and what comes out.

Example

Imagine you are trying to reach the bottom of a valley while blindfolded. At each step you check how steep the ground is where you stand, then aim for the spot where a straight line along that slope would hit the ground, and step there. Each step gets you closer, just as each Newton-Raphson guess gets closer to the square root.

3 checked practice questions for this part are in the app.

Part 8 · 1:10:00–1:14:20

This part explains how breaking code into small reusable pieces and hiding unneeded details makes code easier to fix, maintain, and understand.

Key ideas

Words to know

decomposition
Breaking a big job into smaller pieces of code that each do one part of the job.
abstraction
Hiding the details you do not need to see, so you can focus on what a piece of code does.
module
A separate, self-contained piece of code that you can use together with other pieces to reach a shared goal.
function
A small reusable piece of code that you write once and can call again and again with different inputs.

Example

Think of a group project where each person solves one part and then brings their result to the group. You do not need to know how your teammate solved their part, only that they solved it correctly. Abstraction works the same way: you care about what the code gives back, not every detail of how it got there.

3 checked practice questions for this part are in the app.

← Lecture 4: Loops over Strings, Guess-and-Check, and Binary · Lecture 7: Decomposition, Abstraction, and Functions →

Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. MIT OpenCourseWare · CC BY-NC-SA 4.0 · not affiliated with or endorsed by MIT. These notes are shared under the same license.

LessonBound · Free lecture notes · Privacy · Terms · New York