Open the contacts app on your phone and type the first letters of a friend's name. The name pops up before you finish typing. Now picture the Election Commission's voter list: twelve crore names, kept in alphabetical order. Someone types a name and the answer must still come back at once. Your first guess might be that it needs a much faster computer. It doesn't. It needs a smarter way of looking. If the program checked the names one by one from the top, it could end up making twelve crore comparisons. A program that opens the list in the middle, throws away the half that cannot hold the name, and repeats, never needs more than 27.
The same two programs, written by two students, would both pass a test on a hundred names. They part ways only when the data grows. This chapter is about seeing that difference coming before you run anything, and about the small language we use to say it: time complexity, space complexity, and Big-O notation. Every later chapter of Data Structures leans on it, because every structure you will meet (array, list, stack, tree, hash table) is judged by what its operations cost.
By the end of this chapter you will be able to count the work a piece of code does and write its Big-O with the reason. You will define O, Ω and Θ properly, prove a bound with constants, and rank the usual growth classes from memory. You will find the call-stack space of a recursive function, write the recurrence of a divide-and-conquer algorithm and solve it with the Master Theorem, explain why quicksort can be slow, and show why a dynamic array's push is cheap on average even though one push can be expensive.
We go in six parts, from easy to hard: what we measure and the tools we need (logarithms and two sums); the notations and the ladder of growth rates; reading Big-O off loops; space and the call stack; recurrences and the Master Theorem; and finally amortised cost.