🌍 CS50x: Introduction to Computer Science · subject
CS50x: Introduction to Computer Science Algorithms Syllabus
The Algorithms syllabus for CS50x: Introduction to Computer Science in full: 4 chapters and 9 topics, every one listed below, plus 50 Algorithms flashcards with answers.
Algorithms syllabus: all 4 chapters
Expand any chapter to see its topics and sub-topics.
-
Searching
2 topics- Linear Search
- Binary Search
-
Sorting
3 topics- Bubble Sort
- Selection Sort
- Merge Sort
-
Algorithmic Efficiency
2 topics- Asymptotic Notation
- Running Time Analysis
-
Recursion
2 topics- Recursive Functions
- Base Cases
CS50x: Introduction to Computer Science Algorithms flashcards — 50 questions with answers
What is linear search?
A search algorithm that checks each element of an array one at a time, from the first to the last, until the target value is found or the end of the array is reached.
What is the worst-case running time of linear search on an array of $n$ elements?
$O(n)$ — in the worst case the target is the last element or absent, so all $n$ elements must be checked.
What is the best-case running time of linear search?
$\Omega(1)$ — in the best case the target is the very first element checked.
Does linear search require the array to be sorted?
No. Linear search works on any array, sorted or unsorted, because it simply inspects every element in order.
State the basic procedure (pseudocode idea) of linear search for a target $t$.
For each element from index $0$ to $n-1$: if the current element equals $t$, return its position (success); if the loop ends without a match, report that $t$ is not present.
What is binary search?
A search algorithm for sorted arrays that repeatedly compares the target to the middle element and discards the half of the array that cannot contain the target, halving the search space each step.
What precondition must hold before binary search can be used?
The array must be sorted. On an unsorted array, discarding half of the elements could throw away the target, giving wrong results.
What is the worst-case running time of binary search on $n$ elements?
$O(\log n)$, because each comparison halves the remaining search space.
What is the best-case running time of binary search?
$\Omega(1)$ — the target might be exactly the middle element on the first comparison.
Describe the steps of binary search for a target $t$ in a sorted array.
1) Look at the middle element. 2) If it equals $t$, done. 3) If $t$ is smaller, repeat the search on the left half; if larger, repeat on the right half. 4) If the remaining range becomes empty, $t$ is not present.
After $k$ steps of binary search on $n$ elements, how many elements can remain in the search space?
At most $\frac{n}{2^{k}}$, since each step halves the search space. The search ends when $\frac{n}{2^{k}} \leq 1$, i.e. after about $\log_{2} n$ steps.
Approximately how many comparisons does binary search need in the worst case for an array of $1{,}000{,}000$ elements?
About $20$, because $2^{20} = 1{,}048{,}576 \geq 10^{6}$, so $\log_{2}(10^{6}) \approx 20$.
In the classic phone book example, why is repeatedly tearing the book in half an $O(\log n)$ algorithm?
Each tear discards half of the remaining pages, so a book of $n$ pages needs only about $\log_{2} n$ tears to narrow down to one page — doubling the book size adds just one extra step.
What is bubble sort?
A sorting algorithm that repeatedly steps through the array, compares adjacent pairs of elements, and swaps them if they are out of order, so larger values "bubble" toward the end on each pass.
What is the worst-case running time of bubble sort?
$O(n^{2})$ — roughly $(n-1)$ passes each doing about $(n-1)$ comparisons, i.e. $(n-1)(n-1) = n^{2} - 2n + 1$ comparisons.
What is the best-case running time of bubble sort (with the early-exit optimization)?
$\Omega(n)$ — if the array is already sorted, one full pass makes no swaps, and the algorithm can stop after that single pass of $n-1$ comparisons.
What optimization lets bubble sort terminate early?
Keep a counter or flag for swaps during a pass; if a complete pass makes zero swaps, the array is already sorted and the algorithm can stop immediately.
Describe one full pass of bubble sort.
Move from index $0$ to $n-2$: compare each element with its right neighbor, and swap the pair if the left element is larger. After the pass, the largest unsorted element has reached its final position at the end.
After the first complete pass of bubble sort, what is guaranteed about the array?
The largest element has "bubbled" to the last position; after $k$ passes, the largest $k$ elements are in their final sorted positions at the end.
What is selection sort?
A sorting algorithm that repeatedly scans the unsorted portion of the array for its smallest element and swaps it into the leftmost unsorted position, growing a sorted region from left to right.
Show the other 30 Algorithms flashcards
What is the worst-case running time of selection sort?
$O(n^{2})$ — it performs $(n-1) + (n-2) + \cdots + 1 = \frac{n(n-1)}{2}$ comparisons.
What is the best-case running time of selection sort, and why?
$\Omega(n^{2})$ — even on an already sorted array, selection sort must still scan the entire unsorted portion every round to confirm the minimum, so it cannot exit early.
Give the formula for the total number of comparisons selection sort makes on $n$ elements.
$$\sum_{k=1}^{n-1} k = \frac{n(n-1)}{2} = \frac{n^{2}}{2} - \frac{n}{2},$$ which is $O(n^{2})$ after dropping constants and lower-order terms.
Describe the steps of selection sort.
For $i$ from $0$ to $n-2$: search positions $i$ through $n-1$ for the smallest element, then swap it with the element at position $i$. After each round, the sorted region at the front grows by one element.
Compare bubble sort and selection sort on a fully sorted input.
Both are $O(n^{2})$ in the worst case, but optimized bubble sort detects the sorted input and finishes in $\Omega(n)$, while selection sort still takes $\Omega(n^{2})$ because it always scans for the minimum.
What is merge sort?
A recursive divide-and-conquer sorting algorithm: split the array into two halves, recursively sort each half, then merge the two sorted halves into one sorted array.
State merge sort in three lines of pseudocode.
If the array has only $1$ element, it is already sorted (return). Otherwise: sort the left half, sort the right half, then merge the two sorted halves.
What is the worst-case running time of merge sort?
$O(n \log n)$ — asymptotically faster than the $O(n^{2})$ of bubble sort and selection sort.
What is the best-case running time of merge sort?
$\Omega(n \log n)$ — merge sort always splits and merges fully, so even a sorted input takes $n \log n$ time; hence merge sort is $\Theta(n \log n)$.
Why does merge sort run in $O(n \log n)$ time?
Halving the array repeatedly creates about $\log_{2} n$ levels of division, and merging at each level touches all $n$ elements, giving total work $n \times \log_{2} n$.
What is the base case of merge sort?
An array (or subarray) of size $1$: a single element is already sorted, so the recursion stops and returns immediately.
How does the merge step combine two sorted halves?
Repeatedly compare the front (smallest remaining) elements of the two halves, copy the smaller one into the output, and advance in that half; when one half is exhausted, copy over the rest of the other half.
What is the main trade-off merge sort makes for its speed?
Extra memory: it needs additional space (on the order of $O(n)$) to hold elements while merging, whereas bubble sort and selection sort sort in place.
What does Big $O$ notation describe?
An upper bound on running time — how the algorithm's time grows with input size $n$ in the worst case (e.g. linear search is $O(n)$).
What does Big $\Omega$ (Omega) notation describe?
A lower bound on running time — the best case for the algorithm (e.g. linear search is $\Omega(1)$, since the target might be found immediately).
What does Big $\Theta$ (Theta) notation describe, and when does it apply?
A tight bound: when an algorithm's upper and lower bounds coincide, i.e. $O$ and $\Omega$ are the same, we write $\Theta$ (e.g. selection sort is $\Theta(n^{2})$, merge sort is $\Theta(n \log n)$).
Order these common running times from fastest to slowest: $O(n^{2})$, $O(1)$, $O(n \log n)$, $O(n)$, $O(\log n)$.
$$O(1) < O(\log n) < O(n) < O(n \log n) < O(n^{2})$$
Why is an algorithm taking $\frac{n^{2}}{2} - \frac{n}{2}$ steps still classified as $O(n^{2})$?
Asymptotic notation ignores constant factors and lower-order terms because, as $n \to \infty$, the $n^{2}$ term dominates growth; only the dominant term matters.
What does a running time of $O(1)$ mean? Give an example.
Constant time: the number of steps does not grow with input size $n$. Example: accessing an array element by index, or checking the first element of a list.
Give the worst-case ($O$) and best-case ($\Omega$) running times of linear search and binary search.
Linear search: $O(n)$ and $\Omega(1)$. Binary search: $O(\log n)$ and $\Omega(1)$. Binary search is faster in the worst case but requires sorted data.
Give the worst-case ($O$) and best-case ($\Omega$) running times of bubble sort, selection sort, and merge sort.
Bubble sort: $O(n^{2})$, $\Omega(n)$. Selection sort: $O(n^{2})$, $\Omega(n^{2})$. Merge sort: $O(n \log n)$, $\Omega(n \log n)$.
If you double the input size $n$ of an $O(n^{2})$ algorithm, roughly how much longer does it take?
About $4$ times longer, since $(2n)^{2} = 4n^{2}$.
When is it worth sorting an array first ($O(n \log n)$ with merge sort) rather than using linear search directly?
When you will search the data many times: sorting once costs $O(n \log n)$, but then every search is only $O(\log n)$ with binary search instead of $O(n)$ each time.
What is a recursive function?
A function that calls itself, solving a problem by reducing it to one or more smaller instances of the same problem until a trivially solvable case is reached.
What two essential parts must every correct recursive function have?
1) A base case: a condition where the function stops calling itself and returns directly. 2) A recursive case: a call to itself on a smaller version of the problem that moves toward the base case.
What is a base case in recursion, and what is its purpose?
The simplest input for which the answer is returned directly without further recursive calls. It terminates the recursion, guaranteeing the chain of self-calls eventually stops.
What happens if a recursive function has no (reachable) base case?
It calls itself forever: each call adds a new frame to the call stack until memory is exhausted, causing a stack overflow and crashing the program (e.g. a segmentation fault in C).
Define the factorial function recursively, including its base case.
$$n! = n \cdot (n-1)! \quad \text{for } n \geq 1, \qquad 0! = 1.$$ The base case $0! = 1$ (equivalently $1! = 1$) stops the recursion.
Describe binary search as a recursive function, naming its base cases.
Recursive case: compare the target to the middle element and recurse on the left or right half. Base cases: the middle element equals the target (found), or the search range is empty (not found).
Which of the algorithms covered in CS50 is naturally recursive, and what makes it so?
Merge sort: it solves the problem by calling itself on the left half and the right half of the array (recursive cases), stopping when a subarray has one element (base case), then merging results.
CS50x: Introduction to Computer Science Algorithms FAQ
What is in the CS50x: Introduction to Computer Science Algorithms syllabus?
CS50x: Introduction to Computer Science Algorithms is split into 4 chapters: Searching, Sorting, Algorithmic Efficiency and Recursion. Together they contain 9 topics.
How many chapters are in CS50x: Introduction to Computer Science Algorithms?
4 chapters, containing 9 topics. The largest chapter is Sorting, with 3 topics.
Are there flashcards for CS50x: Introduction to Computer Science Algorithms?
Yes — a 50-card Algorithms deck, printed in full in the flashcards section of this page and available in the Examius app with spaced repetition. The free plan covers one exam of your choice, including its flashcards; Examius Pro, a one-time purchase, unlocks every other exam and deck.