Lecture notes · Algorithms · Lecture 3

Sets and Sorting

MIT 6.006 Introduction to Algorithms, lecture 3, 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 introduces the set, a container that holds items you can add, search, and remove, and explains how an interface differs from the data structure that stores it.

Key ideas

Words to know

interface
A list of things you can do with an object, written as a plan, without saying how the object does them.
data structure
The way a computer actually stores information and carries out the operations listed in an interface.
key
The one piece of information you search by, like a student ID number. Other details about the item are not used for searching.
dynamic
Changing over time. A dynamic operation changes what is inside the set, such as adding or removing an item.

Example

Think of a class roster where each student's ID number is the key. You can ask whether a student ID is on the list, and if it is, you get back that student's name and other details. The roster lets you add or remove students, but the list of things you can do is the same no matter how the roster is kept.

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

Part 2 · 10:00–20:00

This part explains why the way you store a set of items changes how fast you can search it, and why sorting the items is useful but costs extra work up front.

Key ideas

Words to know

order n time (linear time)
The time a task takes grows in step with the number of items. Double the items, and the task takes about twice as long.
binary search
A way to find an item in a sorted list by checking the middle, then throwing away the half that cannot contain it, and repeating.
key
The value you use to decide where an item goes in a sorted list, such as a student ID number.
data structure
A way of organizing data in a computer so that certain tasks, like searching or adding items, are fast or slow.

Example

Think of looking up a name in a pile of unsorted papers versus a phone book. In the pile, you may need to check every page. In the phone book, you open near the middle, see if you need to go earlier or later, and keep halving until you find the name.

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

Part 3 · 20:00–30:00

This part introduces key sorting words and a silly first sorting method, then starts explaining selection sort.

Key ideas

Words to know

destructive
A sort that changes the original list itself, instead of making a new sorted copy.
in place
Sorting that changes the list directly and does not need extra memory that grows with the list size.
permutation
One of the different orders you can arrange a set of items in.
lower bound
A minimum amount of time (or work) that something must take. It tells you the least it could need, not the exact amount.

Example

Imagine lining up people by height, but you can only use the line itself and no extra space. You find the tallest person and move them to the end, then find the next tallest and move them just before that, and so on. Each move makes the end of the line a little more sorted.

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

Part 4 · 30:00–40:00

This part shows how to find the biggest item in a list, how to prove that method works, how to measure how long it takes, and how that helps build a sorting method called selection sort.

Key ideas

Words to know

induction
A way of proving something works for every size. You show it works for the smallest case, then show that if it works for one size, it works for the next size up.
recursive
When a function calls itself. Each call usually handles a smaller version of the same problem.
substitution
A proof method for runtime. You guess a formula, like cn, put it into the recursive rule, and check that both sides match.
theta
A way of writing how the time grows as the input grows, ignoring constant factors. Theta of 1 means the work stays the same size no matter how big the input is.

Example

Think of finding the tallest kid in a line by walking from the front. At each step you ask one question: is the kid at the end taller than the tallest kid I have already seen? Your answer is the taller of those two, so the work grows one step per kid.

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

Part 5 · 40:00–52:51

This part shows why selection sort is too slow and how merge sort sorts a list faster by splitting it in half, sorting each half, and joining the halves together.

Key ideas

Words to know

recursive
A recursive call is when a function calls itself on a smaller piece of the problem, like sorting the left half of a list by using the same steps again.
substitution method
A way to prove how long an algorithm takes by guessing a formula for its running time, then plugging it back in to check that the guess fits.
linear time
A running time that grows in step with the size of the list. Doubling the list roughly doubles the work.
power of 2
A number you get by multiplying 2 by itself, such as 2, 4, 8, 16. The lecture assumes list lengths are like this to keep the math simple.

Example

Imagine two piles of playing cards, and each pile is already sorted from smallest to largest. To make one sorted pile, you look at the top card of each pile, place the smaller card on the table, and repeat until both piles are empty. This is the same idea as the two finger merge in the lecture.

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

← Lecture 2: Data Structures and Dynamic Arrays · Lecture 4: Hashing →

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