Big O Notation
Time and Space Complexity Explained With Python Code You Can Measure, 40 Practice Problems and Answers
-
- $24.99
-
- $24.99
Publisher Description
You have met O(n) and O(n²) in textbooks and code reviews, and you may still be unsure what they promise or how to check them. This book makes the notation concrete: you count the steps your code takes, name how that count grows, and then confirm it with a stopwatch on your own computer, in plain Python.
Every chapter works the same way. You predict the growth class from the code, count the steps with a counter that gives the same numbers on every machine, and time the code at doubling sizes to see the curve. A small helper module built in the first chapter does the timing, and every claim comes with a listing you can run and a printed result.
Inside you will find:
• Counting steps instead of seconds, and the doubling test that turns timings into a growth rate
• The growth curves from constant to exponential, and a budget showing how large n can be for each
• Seven rules for reading loops, nested loops and the hidden loops inside single calls
• Best, worst and average cases, and why Big O is not a synonym for worst case
• What lists, dictionaries, sets, deques, heaps and sorted lists cost, with a probe script that checks the table on your own machine
• Searching and sorting compared by exact comparison counts
• Recursion, recurrences and the recursion tree, with call counts for plain and cached functions
• Amortized cost, and why appending to a list is cheap on average and slow now and then
• Space complexity and measuring memory
• Profiling, the places where Big O misleads, a seven-step method for making a slow program faster and a one-page cheat sheet
• 40 practice problems with answers
It is written for students, self-taught programmers, career changers and working developers who can already write Python functions with loops and lists. The timings come from one ordinary personal computer and will differ on yours; the counts and the shapes of the curves will not.
Written as general information and practice material. Details of the language and its standard library change between versions, so check the current documentation of the version you use.