Algorithm Analysis
with Python
Two programs can give exactly the same answer, on exactly the same computer, and one can finish before you blink while the other runs until next Tuesday. This course is about noticing that difference, measuring it, naming it, and making better choices because of it — building the Python you need as you go.
01What this course is
Many algorithm courses open with proofs and assume you already program fluently. This one starts from working code and measurement. Weeks 1–4 build the slice of Python the subject needs — printing, variables, loops, lists — so nobody is left behind; from week 5 onward every new idea is introduced the same way: guess, measure, explain, name. If you already program, skim the first weeks and start at week 5.
By the end you will not be a software engineer, and that is fine. You will be someone who can look at two ways of doing a job and say, with evidence, "this one gets slower much faster than that one, and here is why." That skill transfers to spreadsheets, lab data, business processes and everyday decisions — it is not really about Python at all.
Students in any field who are early in their programming; researchers who inherited a script that is "too slow"; anyone curious about why software gets sluggish exactly when the data gets interesting. No mathematics beyond multiplication and a willingness to look at a graph.
02What you will be able to do
Read a program as steps
Take any small piece of code and count the work it does, instead of trusting your gut.
Measure honestly
Time code with repeats, plot time against input size, and know when a measurement is lying to you.
Speak Big-O
Recognise O(1), O(log n), O(n), O(n log n) and O(n²) in code you wrote yourself, and say what each means in plain words.
Pick the right container
Know when a list is the wrong tool and a set or dictionary makes a program a thousand times faster.
Spot the classic traps
Recognise the handful of everyday patterns that quietly turn a fast program into a slow one.
Defend a choice
Produce a short report: here is the problem, here are two approaches, here is the measurement, here is my recommendation.
03How a week works
Every week page follows the same shape, so you always know where you are:
- The big question — one sentence at the top of the page. Everything that week answers it.
- Plain-language explanation — with an everyday analogy before any code appears.
- Code you can run — short, complete, copy-paste into Colab. Never more than ~15 lines at a time.
- Predict, then run — you write down your guess before executing. Being wrong is the useful part.
- Try it yourself — small tasks with hints and full solutions hidden behind a click.
- Self-check quiz — instant feedback, no grades, unlimited attempts.
- Homework — one deliverable per week, usually a notebook plus five sentences of interpretation.
- Chapter problem set — a batch of exercises from Skiena's Algorithm Design Manual (Ch. 2), matched to the week and fully worked out at this course's level. By week 14 the whole chapter is solved. Harder proof-style or interview problems are marked ★ optional challenge — worth reading, never required. Every one of them is also worked out, even more simply, in the Skiena Chapter 2 solutions companion (a printable booklet).
Never let a code block go by without running it. Reading about running time is like reading about swimming. The whole course is built around a stopwatch and a plot, and neither works from the sofa.
04Tools you need
| Tool | Why | Cost / setup |
|---|---|---|
| Google Colab | Runs Python in a browser tab. Nothing to install. | Free, needs a Google account |
| A browser | These pages, and Colab. | You have one |
| Paper or a text file | Writing predictions down before running code. This is not optional. | Free |
Nothing in this course needs a fast computer. A slow laptop is arguably a better teacher: the differences show up sooner.
05The 14 weeks
The first four weeks are pure "learn just enough Python". The last ten are the actual subject. If you already program a little, skim weeks 1–4 and start at week 5.
What Is an Algorithm?
Recipes, step-by-step thinking, and why two correct methods can be wildly different.
✓Your First Python: Values, Names, and Output
print, variables, numbers, text — the four things you need before anything else.
✓Repeating Work: Loops and a Step Counter
for, range, and while — plus counting how many steps your program really takes.
✓Lists: Holding Many Things at Once
Making lists, reading items, searching with in — and feeling work grow with size.
✓Functions and the Stopwatch
Wrap work in a function, then time it honestly with perf_counter and repeats.
✓The Doubling Experiment
Double n, look at the time: flat, twice as slow, or four times as slow?
✓Counting Steps Instead of Seconds
Seconds depend on your laptop; step counts don't. Meet T(n) and the dominant term.
✓Big-O Notation
One short symbol for a growth story: O(1), O(log n), O(n), O(n log n), O(n²), O(2ⁿ).
✓One Problem, Four Algorithms: The Anagram Story
The classic case study — four correct solutions, four completely different costs.
✓What Python Lists Really Cost
append vs insert(0), pop() vs pop(0), + vs comprehension — measured, not guessed.
✓Dictionaries and Sets: Lookup That Doesn't Slow Down
Hashing intuition and the single biggest speed win a beginner can learn.
✓Searching: Linear vs Binary
Guess-the-number, the phone book trick, and why log n barely grows.
✓Sorting: Why Some Sorts Are Slow
Bubble and selection sort by hand, then Python's sorted() — n² vs n log n on a plot.
✓Putting It All Together
A checklist for choosing, the classic 'accidentally quadratic' traps, and the final project.
✓06Assessment (suggested)
| Component | Weight | What it is |
|---|---|---|
| Weekly homework (12 × ) | 36% | One notebook per week; graded pass / redo / good. |
| Self-check quizzes | 4% | Completion only — they exist to catch confusion early. |
| Midterm (week 8) | 25% | Read short code, count the steps, name the growth, justify in one sentence each. |
| Final project (weeks 12–14) | 35% | Two approaches to one problem, benchmarked, plotted, and recommended in a two-page report. |
There is no closed-book memorisation of complexity tables. Every table you would need is on these pages; the exam asks you to use it.
07The final project
Pick any small task with real data — searching a list of student records, counting words in a book, matching two spreadsheets, de-duplicating a mailing list. Then:
- Solve it the obvious, first-thing-that-comes-to-mind way.
- Solve it a second way using something you learned after week 9.
- Benchmark both at four input sizes and plot the two curves on one figure.
- Name the growth of each in Big-O and say why the curves look the way they do.
- Write two pages: problem, approaches, evidence, recommendation, one honest limitation.
The faster program does not automatically win. A project that measures carefully, explains an unexpected result and admits what it could not test scores higher than one that is fast but hand-waves.
09For mechatronics students: the engineering payoff
This course is taught in a Mechatronics Engineering department, and that is no accident. In a robot or a controller an algorithm must not only be correct — it must finish before the next sensor sample arrives, on a chip with kilobytes of memory. The engineering capstone turns every week of this course into the machines you will actually build: control-loop deadlines, lookup tables burned into flash, the FFT for vibration monitoring, circular buffers, and why you cannot simply "buy a faster computer" when the computer is bolted to the robot.
The capstone assumes the whole course. Work through weeks 1–14 first, then read it to see why the whole thing mattered for your degree. It is linked at the foot of week 14, or here: Why Algorithm Analysis Belongs in Mechatronics →
08Sources and further reading
- Miller & Ranum, Problem Solving with Algorithms and Data Structures using Python — the Algorithm Analysis chapter is the backbone of weeks 7–11, including the anagram case study and the Python operation-cost tables. Free and interactive.
- The Python documentation on time complexity of built-in operations — the reference table behind weeks 10 and 11.
- Steven Skiena, The Algorithm Design Manual — the Chapter 2 exercises are woven through the weekly problem sets and solved at a beginner level, so students meet a respected professional reference without being thrown in the deep end.
- For students who want the next step after this course: the companion Data Structures & Algorithms course, which assumes everything here.
This course is deliberately narrower and slower than the textbook it draws on: fewer topics, more measuring, and every example rewritten for someone whose first line of Python was written in week 2.