Lecture notes · Programming in Python · Lecture 22

Big Oh and Theta

MIT 6.100L Intro to CS and Programming using Python, lecture 22, 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 shows how to measure how long a program takes to run, and how changing the inputs can change that time.

Key ideas

Words to know

performance counter
A timer in Python that measures very short amounts of time. It is used to find out how long a program or function takes to run.
precision
How small a measurement can be and still be shown accurately. Higher precision means finer detail.
scientific notation
A short way to write very large or very small numbers using powers of 10, such as 4.3 times 10 to the negative 6.
parameter
An input that a function accepts, like the miles given to convert_to_kilometers.

Example

Think of timing a race with a stopwatch. A watch that shows only whole seconds will read zero for a very quick sprint, but a watch that shows hundredths of a second can catch the real time. The performance counter works like the more precise watch.

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

Part 2 · 10:00–20:00

This part compares how the run time of programs grows as their input gets bigger, using a compound interest function and list functions.

Key ideas

Words to know

input
The information you give to a function so it can do its work, such as a number or a list.
for loop
A set of steps the computer repeats once for each item or count, such as going through each number in a list one at a time.
brute force
A method that tries every option one at a time, without any clever shortcuts.
algorithmically
Looking at how a method works step by step, and how its run time grows, rather than at what the method is for.

Example

Think of a cashier who scans each item in your cart one at a time. If you have 10 times as many items, it takes about 10 times as long to check out, no matter what the items are. The cashier's speed for each item is what decides how the total time grows.

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

Part 3 · 20:00–30:00

This part compares three ways to check whether an item is in a list, and shows that binary search is far faster than checking one item at a time.

Key ideas

Words to know

binary search
A search that checks the middle item of a sorted list and then throws away the half that cannot contain the answer, repeating until it finds the item or runs out of list. It is also called bisection search.
linear
Growing at the same rate as the input. If the list gets 10 times bigger, the time also gets about 10 times bigger.
integer division
Dividing and then rounding down to a whole number, written with // in Python. It is used here so the middle position is a whole number.
built-in
Already part of Python, so you do not have to write it yourself. The in keyword is an example.

Example

Imagine finding a name in a printed phone book. You open to the middle, see whether the name comes before or after that page, and tear away the half that cannot have it. Repeating this finds the name in far fewer steps than reading every name from the first page.

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

Part 4 · 30:00–40:00

This part explains a function that finds the longest distance between any two points, and why its running time grows much faster as the input gets bigger.

Key ideas

Words to know

nested for loops
One loop that runs inside another loop, so the inner loop finishes all its steps for each step of the outer loop.
tuple
A small group of values kept together in one pair or set, like the x and y position of one point.
magnitude
How big something is. Here it means how large a distance is.
quadratic
A pattern where doubling the input makes the work about four times as large, so the time grows faster and faster.

Example

Think of a group of friends at a party who each want to shake hands with every other friend once. The first person shakes hands with everyone else, then the second person shakes hands with the people who are left, and so on. Adding a few more people makes the total number of handshakes grow much faster than the number of people, just as the diameter function's work grows faster than its input.

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

Part 5 · 40:00–50:00

This part explains why timing a program alone is not enough, and how counting basic steps gives a formula for how long a program will take as its input grows.

Key ideas

Words to know

asymptotic complexity
A way of describing how the work a program does grows as the input grows, without having to run it.
operation
One small basic step in a program, such as an addition, a comparison, or an assignment.
global variable
A variable defined outside any function that every function in the program can read and change.
logarithmic
A pattern where the work grows slowly, so each big jump in input adds only a little more work.

Example

Think of counting the steps you take to walk a hallway. If the hallway has 10 doors and you check each one, the total steps grow in a steady way with the number of doors. Counting steps like this lets you predict how long the walk will take without actually walking it.

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

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

This part explains how to describe how fast a program's work grows as its input gets bigger, and why we ignore small details to do it.

Key ideas

Words to know

order of growth
How fast the amount of work rises as the input gets bigger.
worst case
The hardest situation a program could face, which this class always measures.
big O
A notation that writes down an upper bound on how fast a program's work grows.
logarithmic
A growth pattern that rises slowly, the kind the lecture found for binary search.

Example

The lecturer checks whether 0 is in a list of 3 numbers, then in a list of 10 numbers. The longer list can take more steps, so the list length matters. The actual numbers inside do not change the work, so they are left out of the measurement.

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

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

This part explains how to describe how fast a function grows, using big O (an upper limit) and theta (an upper and lower limit) by keeping only the fastest-growing term.

Key ideas

Words to know

big O
A way to say a function grows no faster than some simpler function, once the input is big enough. It is an upper limit.
theta
A tighter way to describe growth. A simple function that is both an upper limit and a lower limit for the original function.
upper bound
A function that sits above the one you are studying, at least for large inputs, like a ceiling over it.
dominant term
The part of a formula that grows fastest and eventually matters most when the input gets very large.

Example

Think of a phone bill that is 3 times your minutes squared plus 20 times your minutes plus 1. For a very long call, the squared part takes over and mostly decides the total, so the bill is best described by the squared part alone. The smaller pieces and the multiplier matter much less at that scale.

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

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

This part shows how to describe how a program's running time grows, using Big Oh-style labels called theta, based on the input the program takes.

Key ideas

Words to know

theta
A label that says how fast a program's running time grows as its input gets bigger. It ignores small fixed details and keeps the main growth pattern.
dominant term
The part of a formula that grows the fastest, so it matters most when the input is large. For example, in n squared plus n, the n squared is the dominant term.
multiplicative constant
A fixed number multiplied in front of a variable, like the 5 in 5n. Theta ignores these numbers.
polynomial
A running time where the input is raised to a fixed power, like n squared or n cubed. These get slow quickly as the input grows, though they are less slow than exponential ones.

Example

Think of a checkout line where each person takes the same time to serve. If there are L people, the total time grows in step with L, so you would call it theta of L. If you added a second line that you had to finish after the first, you would add the two times and keep only the bigger part.

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

← Lecture 21: Timing Programs and Counting Operations · Lecture 24: Sorting Algorithms →

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