Week 3: Algorithms


Week 3: Algorithms

Maria
Alright Simon, this week's lecture is all about algorithms. And it starts right where we left off in week 0 — with that phone book and searching for John Harvard. Remember tearing pages in half?

Simon
How could I forget? We had three algorithms: one page at a time, two pages at a time, and the divide-and-conquer one they call binary search. And the big idea was that binary search is way faster because it cuts the problem in half each time.

Maria
Exactly. And the key is that the data has to be sorted for binary search to work. That's why the phone book is alphabetized. But if you have unsorted data, you're stuck with linear search — checking every element one by one. That's O(n) in the worst case.

Simon
O(n) — we finally give it a name. Big O notation. So linear search is O(n), binary search is O(log n). But there's also best case, which we call big Omega. For linear search, best case is Omega(1) — you get lucky and find it first try.

Maria
And for binary search, best case is also Omega(1) if the middle door happens to have the number. But worst case is O(log n). When the upper and lower bounds are the same, we use big Theta. But not all algorithms have matching bounds.

Simon
Right. So they moved from theory to code. First, they wrote a simple linear search in C for integers. Just a for loop checking if numbers[i] equals the target. If found, return 0; otherwise, after the loop, return 1. Simple and correct.

Maria
But when they tried to search for strings, equals equals didn't work. That's because strings aren't simple values — they're arrays of characters. So you have to use strcmp from string.h, which returns 0 if the strings are equal. That was a nice catch.

Simon
Then they built a phonebook with two parallel arrays — one for names, one for numbers. But that's fragile. If you add or remove a person, you have to keep both arrays in sync. So they introduced a custom data type: a struct called person with name and number fields.

Maria
Yeah, that's much better. You declare typedef struct { string name; string number; } person; then you create an array of person. To access a field, you use the dot operator — like people[i].name. It bundles related data together.

Simon
And they noted that phone numbers should be stored as strings, not integers, because you don't do math on them and they can have hyphens or parentheses. Good rule of thumb.

Maria
Now the big topic: sorting. Because if you want to use binary search, you need sorted data. They demonstrated two simple sorting algorithms with volunteers on stage: selection sort and bubble sort.

Simon
Selection sort: find the smallest element in the unsorted part and swap it with the first element of that part. Repeat. It takes about n² steps in both best and worst case — so it's Theta(n²).

Maria
Bubble sort: repeatedly step through the list, swapping adjacent elements if they're out of order. Each pass bubbles the largest unsorted element to the end. With an optimization — stop if no swaps — best case becomes Omega(n) but worst case is still O(n²).

Simon
But they showed that even with optimization, bubble sort is slow. So then comes merge sort — a recursive algorithm that's much faster: O(n log n). The idea: sort the left half, sort the right half, then merge the two sorted halves.

Maria
They explained recursion using a pyramid example. A pyramid of height n is just a pyramid of height n-1 plus one more row. In code, you draw a pyramid of height n by calling draw(n-1) and then printing a row of n hashes. Base case: if n == 0, just return.

Simon
That's elegant. And merge sort works the same way: to sort an array of size n, recursively sort the left half and right half, then merge. Merging two sorted lists takes O(n) because you only look at each element once.

Maria
They visualized it with a demo: splitting the array down to single elements, then merging back up. At each level, you do n comparisons, and there are log n levels, so total O(n log n). But it uses extra memory for the merge — a space-time tradeoff.

Simon
Right. You can't get something for nothing. If you want speed, you often need more space. And they ended with a dramatic music video comparing selection sort, bubble sort, and merge sort — merge sort won hands down.

Maria
The takeaway: algorithms matter. Linear search or binary search, simple sorts versus merge sort — choosing the right one can make your programs run in seconds instead of hours. And now we have the tools to analyze them: big O, Omega, and Theta.

Ask anything
We've read this whole study set, so questions about it get the best answers.