Algorithms course
Designing and analyzing algorithms.
Topics in this course
1.Big-O Notation
Big-O notation describes how an algorithm's running time or memory grows as the input size grows, ignoring constant factors and lower-order terms. An algorithm that takes steps is , because for large the term dominates everything else.
- Drop constants and lower-order terms: is .
- Sequential steps add; nested loops multiply.
- Halving the problem each step gives .
Quick check: About how many halvings take 1024 items down to 1?
Includes a worked example, 3 common mistakes and 3 practice questions with answers. Study big-o notation
2.Searching and Sorting
Searching finds an item in a collection. Linear search checks each element in turn and works on any list in time. Binary search works only on sorted data: it checks the middle element and discards half the list each step, finding any item among a million in about 20 comparisons.
- Binary search needs a sorted list and takes at most comparisons.
- Insertion sort is fast on nearly sorted data.
- Merge sort is always but uses extra memory.
Quick check: Worst-case comparisons for binary search on 1000 sorted items.
Includes a worked example, 3 common mistakes and 3 practice questions with answers. Study searching and sorting
3.Recursion
A recursive function solves a problem by calling itself on a smaller version of the same problem. Every recursive function needs a base case that is answered directly and a recursive case that moves toward it. Factorial is the classic example: , with as the base case.
- Always define the base case first and make sure every call moves toward it.
- Trace with a call tree to see the order of calls and returns.
- Recurrences describe cost: gives ; gives .
Quick check: Compute recursively.
Includes a worked example, 3 common mistakes and 3 practice questions with answers. Study recursion