Lecture notes · CS50: Programming in C · Lecture 3

Algorithms

Harvard CS50 Introduction to Computer Science, 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: the course website.

Part 1 · 0:00–10:00

This part shows why dividing a problem in half again and again is much faster than checking things one at a time, and links that idea to a phone book, a counting game, and contact search.

Key ideas

Words to know

algorithm
A clear, step-by-step set of instructions for solving a problem.
divide and conquer
Solving a problem by splitting it into smaller parts and working on each part, so the work gets smaller at every step.
logarithm
A math idea linked to repeated halving. Each time the problem is halved, the number of steps needed grows only a little, so the work stays small even when the problem is huge.
byte
A small unit of computer memory that holds 8 bits. The lesson describes memory as a grid of these small squares.

Example

Imagine finding one name in a phone book by opening it near the middle, then checking only the half that could hold the name, then halving again. This is the same idea as the standing-up game, where half the group sat down each round. Each step removes half of what is left, so the search finishes quickly.

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

Part 2 · 10:00–20:00

This part explains the search problem: how a computer finds one value in a row of stored numbers, and how knowing the numbers are sorted can change the search.

Key ideas

Words to know

array
A list of values of the same kind, stored one after another in computer memory.
contiguous
Packed together with no gaps, like bytes sitting back to back in memory.
index
The number in brackets that picks out one exact spot inside an array, such as bracket 0 or bracket 3.
linear
Going step by step in a straight line, one item after another, from one end to the other.

Example

Imagine a row of closed mailboxes, and you need to find the one with a letter for you. You cannot see inside any of them, so you must open them one at a time. If the letter is in the first box, you are lucky; if it is in the last, you have to open them all.

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

Part 3 · 20:00–30:00

This part explains two ways to search an ordered row of doors, linear search and binary search, and how to write them as pseudocode.

Key ideas

Words to know

linear search
Searching through items one after another in a straight line, from one end to the other.
binary search
Searching a sorted list by checking the middle item and cutting the list in half again and again.
pseudocode
Plain-English steps written in a code-like style, before you write real code in a programming language.
index
The position number of an item in a list, such as 0 for the first item in an array.

Example

Think of looking up a word in a dictionary. You open it near the middle, see whether your word comes before or after that page, and then ignore the half that cannot hold it. Each look throws away about half of the book.

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

Part 4 · 30:00–40:00

This part explains how computer scientists compare how fast algorithms are, using big O, omega, and theta notation, and then starts writing a linear search in C.

Key ideas

Words to know

big O notation
A way of writing the most steps an algorithm might need, as its upper limit, usually for the worst case. It shows how the work grows as the input grows.
constant factor
A fixed number that multiplies the work, such as 2 times as many steps. Computer scientists ignore these when the input gets large.
omega
A symbol for the lower limit on steps, meaning the fewest steps an algorithm might need, which is its best case.
theta
A symbol used when the upper and lower limits are the same, so the algorithm takes about the same number of steps in both the best and worst cases.

Example

Imagine checking whether a name is on a list of people in a room, one person at a time. In the worst case you check everyone, so the work grows with the size of the room. If everyone stood up at once, the time would stay the same no matter how many people were there, which is a constant-time idea.

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

Part 5 · 40:00–50:00

This part shows how to stop a search early, how main reports success or failure with a number, and why C needs strcmp instead of == to compare strings.

Key ideas

Words to know

exit status
A number a program sends back when it ends. Zero means it finished without problems, and other numbers mean something went wrong.
header file
A file that gives your program access to extra tools, such as string functions, that are not built in by default.
strcmp
A built-in C function that compares two strings. It returns 0 when they are the same.
magic number
A plain number written directly in code, such as 6, with no name to explain what it means. The lesson calls the 6 in the loop a magic number.

Example

Think of a worker who finishes a job and hands in a slip. A slip that says 0 means all went well, and a slip that says 1 or 2 means something went wrong, and the number tells the boss which problem happened. Like main returning a number, the slip is the final report, and the worker stops working once it is handed in.

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

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

This part shows why comparing strings needs a function like strcmp, why phone numbers are stored as text, and how a struct can keep a name and number together in one place.

Key ideas

Words to know

strcmp
A built-in C function that compares two strings. It returns zero if they are equal, and a different number that shows which one comes first if they are not.
ASCII-betical order
The lecturer's playful name for ordering strings by comparing the number codes of their letters. It is close to alphabetical order but not exactly the same.
struct
A way to group several variables together under one name, so one value can hold more than one piece of information, such as a name and a number.
code smell
A hint that something in your code may cause trouble later, even if it works right now. The lecturer says it is a sign to look for a better design.

Example

Think of a paper contact card. The name and phone number are written on the same card, so they always stay together. Two separate lists, one of names and one of numbers, is like keeping them on different sheets of paper: if you add a card to one sheet and forget the other, the names and numbers no longer match.

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

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

This part shows how to group related data into one custom type called a struct, and introduces a methodical, step-by-step way to sort a list of items.

Key ideas

Words to know

struct
A way to group several related pieces of data together under one name, such as a person's name and number.
typedef
A keyword that gives a new, friendlier name to a data type, so you can use that name in your code.
garbage value
A leftover, random value sitting in memory that was never set on purpose, so it cannot be trusted.
pseudocode
Plain-language steps that describe what a program should do, written before the real code is typed.

Example

Imagine a row of volunteers standing in a line, and you must put them in order from smallest number to largest. You cannot see the whole line at once, so you look at one person at a time, remember the smallest number you have seen, and move people into place one step at a time.

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

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

This part explains selection sort, a simple way to sort a list by repeatedly picking the smallest item, and compares it with a second method that fixes neighbouring pairs.

Key ideas

Words to know

array
A row of storage boxes placed side by side in computer memory, each holding one value and numbered by position.
contiguous
Next to each other with no gaps, like a row of seats in a theatre that are all touching.
pseudocode
A short, plain-English outline of the steps an algorithm takes, written so it can later be turned into real code.
selection sort
A sorting method that repeatedly finds the smallest remaining value and moves it into the next spot at the front of the list.

Example

Imagine people standing in a line and asked to order themselves by number. Each time, you look along the whole line for the lowest number, walk that person to the front, and then repeat for the next spot. You only need to remember the lowest number you have seen so far, but you have to walk the line again and again.

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

Part 9 · 1:20:00–1:30:00

This part shows how to measure how many steps selection sort and bubble sort take, and why both grow roughly with the square of the list size.

Key ideas

Words to know

big O notation
A shorthand that describes how the number of steps grows as the list gets bigger. It ignores small details and keeps only the main growth pattern.
dominant term
The part of a formula that becomes the biggest and matters most when the number is large. Smaller parts can be ignored in that case.
asymptotic
Having to do with how something behaves when the input gets very large. Big O, omega and theta are called asymptotic notations.
pseudocode
A plain-English outline of a program written in a loose, code-like style, used before writing real code.

Example

Imagine each of eight people shaking hands with everyone else in the room. That is many handshakes, and the number grows much faster than the number of people. Selection sort works in a similar way, so its work grows quickly as the list gets longer.

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

Part 10 · 1:30:00–1:40:00

This part explains how to stop bubble sort early, and introduces recursion, where a function calls itself on smaller and smaller problems.

Key ideas

Words to know

recursion
When a function calls itself to solve a smaller version of the same problem.
base case
The simple situation where a recursive function stops calling itself, such as when there are no doors left to check.
iteration
Repeating the same steps again and again with a loop, such as going through each row of a pyramid.
best case
The easiest situation an algorithm could face, which shows the fewest steps it might need.

Example

Imagine counting the people in a line by asking the person in front of you to count everyone ahead, then adding one. Each person repeats the same job on a shorter line, and the person at the front, who has no one ahead, simply says zero. That final simple answer is the base case that stops the repeating.

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

Part 11 · 1:40:00–1:50:00

This part shows how to draw a pyramid of hashes using recursion, where a function calls itself, and introduces merge sort as a faster sorting method.

Key ideas

Words to know

recursion
A way of solving a problem where a function calls itself on a smaller version of the same problem.
base case
The simple situation where the function stops calling itself, such as when there is nothing left to draw.
merge sort
A sorting method that splits a list in half, sorts each half, and then stitches the sorted halves back together in order.
array
A list of values stored one after another in memory, such as a list of numbers.

Example

Imagine you want to count a tall stack of books. You can count the stack below the top book, then add one for the top book. The stack below is a smaller version of the same job, and when there are no books left, you stop.

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

Part 12 · 1:50:00–2:01:41

This part shows how merge sort works by splitting a list in half again and again, then merging the pieces back together, and why that makes it much faster than selection sort or bubble sort.

Key ideas

Words to know

recursion
Solving a problem by using the same method on smaller versions of that problem until the pieces are tiny enough to solve directly.
divide and conquer
Breaking a big problem into smaller parts, solving each part, and then combining the answers.
merge
Combining two already sorted lists into one sorted list by repeatedly taking the smaller front number.
big O
A way of describing how the number of steps an algorithm needs grows as the list gets bigger. It ignores constant factors.

Example

Imagine you have a messy pile of 8 playing cards. You split the pile in half, then split each half again, until every pile has one card. Then you combine neighboring piles two at a time, always placing the smaller top card first, until one full sorted pile is left.

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

← Week 2: Arrays · Week 4: Memory →

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

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