Skip to content
Book a Call → mycocoon.life
← Back to Blog Students 11 min read

CS50 Week 3 Explained: Searching, Sorting, Big O and Recursion

Week 3 comes back to the phone book from Week 0 and makes it precise. How do we measure how fast an algorithm is? Why does sorting matter so much? And how can a function solve a problem by calling itself? This is the most "computer science" week of the course.

🎓
An unofficial study guide. CS50 is Harvard University's Introduction to Computer Science, taught by David J. Malan. The lectures, notes and problem sets linked here belong to Harvard and are shared under CC BY-NC-SA 4.0. This guide is Cocoon's own writing and is not affiliated with or endorsed by Harvard. Take the course itself, free, at cs50.harvard.edu/x. Part of our CS50 study series. Keep track of your progress with our free CS50 Study Tracker.
CS50 Week 3 Explained: Searching, Sorting, Big O and Recursion, pixel-art illustration
CS50 Week 3 Explained: Searching, Sorting, Big O and Recursion
Thumbnail of CS50x 2026, Lecture 3: Algorithms
Watch the lecture

CS50x 2026, Lecture 3: Algorithms

2 h 0 min · Harvard University · CS50 on YouTube

Jump to the key moments

  1. 14:27Linear search
  2. 17:40Binary search
  3. 27:01Running time
  4. 56:09Structs
  5. 1:12:20Selection sort
  6. 1:20:12Bubble sort
  7. 1:29:11Recursion
  8. 1:45:46Merge sort
  9. 1:57:23Sort race

Searching: linear vs binary

Linear search checks each item in turn. It works on any list, but on a list of n items it might take n steps. Binary search checks the middle item, discards the half that can't contain the target, and repeats. It takes about log2 n steps, but only if the list is already sorted. That trade-off is the setup for the rest of the lecture.

Running time and Big O

Computer scientists describe speed by how the number of steps grows as the input grows, not in seconds. Big O gives the upper bound (the worst case). Omega (Ω) gives the lower bound (the best case). Theta (Θ) applies when the two are the same. The common ones, from fastest to slowest: O(1) constant, O(log n), O(n), O(n log n), O(n²). Linear search is O(n) but Ω(1), because you might get lucky on the first item. Binary search is O(log n).

Structs: your own data types

A struct bundles related values into one new type. A person might have both a name and a number. Keeping them together means they can't get out of step, which is what happens when you keep two separate arrays and sort only one of them. You'll use structs in every problem this week.

Three sorting algorithms

The "Sort race" chapter at the end of the lecture shows the difference visually. Watch it even if you skip everything else.

Recursion

A recursive function calls itself on a smaller version of the same problem. It needs a base case, the smallest version, which it answers directly. Without one, it calls itself forever and crashes. Merge sort is recursive: to sort a list, sort each half (the recursive part), and a list of one item is already sorted (the base case). If you find recursion confusing, trust the smaller call to do its job and focus only on the current step.

Problem Set 3: what it asks

Where people get stuck

Check yourself

Why can't you use binary search on an unsorted list?

Binary search throws away half the list based on a comparison. That only works if everything on the discarded side is known to be too big or too small.

Which is faster on a list that's already sorted: selection sort or bubble sort?

Bubble sort, if it stops after a pass with no swaps: that's about n steps. Selection sort still does about n² comparisons.

What happens if a recursive function has no base case?

It keeps calling itself until the computer runs out of memory for the calls. That's a stack overflow, and you'll meet it properly in Week 4.

Merge sort is O(n log n) and bubble sort is O(n²). For a million items, roughly how many more steps is that?

n² is a trillion. n log n is about 20 million. So merge sort needs roughly 50,000 times fewer steps.

Algorithms like these run under every AI tool you use. See how retrieval ranks documents in the RAG Lab.

Explore the RAG Lab →