Choose one dominant operation
Comparisons for a search or sort, multiplications for arithmetic. Counting everything changes only the constant.
Nested loops multiply, sequential blocks take the maximum, and a loop whose bound depends on the outer index gives a sum — which is Induction and Recursion's summation formulas doing the work.
Core ideas
Analysis of a loop is counting how many times the body runs, which is usually a sum Induction and Recursion already evaluated.
Comparisons for a search or sort, multiplications for arithmetic. Counting everything changes only the constant.
Two independent nested loops give the product; two blocks in sequence give the sum, which asymptotically is the larger.
for i = 1 to n: for j = 1 to i runs Σ i = n(n+1)/2 times, which is Θ(n²) — the formula of “Induction with Sums and Products”, in Induction and Recursion.
The material
The three patterns, the case distinction, and four worked analyses.
Adding the costs and taking the dominant term. This is the sum rule of “Big-O, Big-Omega and Big-Theta”, and it means a fast block before a slow one contributes nothing asymptotically.
The constant differs from the independent case — half as many iterations — and the class does not. This is where the summation formulas of “Induction with Sums and Products”, in Induction and Recursion, earn their place, and where a triple dependent nest gives Σ i² = Θ(n³).
Any loop whose counter is multiplied or divided by a constant runs logarithmically many times. Recognising that pattern is what identifies the log factors in an analysis.
Worst case is the maximum cost over inputs of size n; best case the minimum; average case the expectation over some distribution of inputs.
Two things stand out. Merge sort and bubble sort have identical bounds in all three cases, for opposite reasons — one is always efficient and the other always wasteful, and neither looks at the data to decide how much work to do. And quicksort's average and worst cases differ by a whole class, which is why the unqualified O(n log n) claim of “Big-O, Big-Omega and Big-Theta” is wrong.
The average case needs a distribution over inputs, and stating it is part of the claim. "Average case Θ(n log n)" for quicksort assumes all input orderings equally likely, which real data frequently violates — nearly sorted input is the classic bad case for a naive pivot.
Interactive
Counts from running, not from quoting
Method
Innermost outward.
The one performed most often: comparisons, multiplications, array accesses. Others differ by a constant factor.
As a function of the outer indices. This is where the dependence shows.
Independent bounds multiply; a dependent bound gives a sum to evaluate with Induction and Recursion's formulas.
Worst, best or average, and for the average say what distribution. An unqualified bound is ambiguous.
Same shape, three different answers.
B is the one worth checking: reindexing the sum by m = n − i + 1 turns it into Σ m, which Induction and Recursion evaluated. Recognising that substitution is most of the technique for dependent nests.
Analyse for i = 1 to n: for j = 1 to i: for k = 1 to j: op and get Σ_i Σ_j j, which is Θ(n³) with a constant of 1/6. The reindexing is the skill.
Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.
No recommendations yet for this lesson.