📘 CodingMarble Learn

Algorithm Complexity: How Fast Does an Algorithm Grow?

Many algorithms can solve the same problem, but some need far more steps. We measure an algorithm by counting its basic steps as the input size n grows, not by stopwatch seconds. Big O notation names the growth: O(1) constant, O(log n) logarithmic, O(n) linear, O(n log n), and O(n²) quadratic. Linear search is O(n), binary search is O(log n); bubble sort is O(n²), merge sort is O(n log n). Memory used is space complexity.

🎬 Step-by-step story

  1. Find the number 13 in 16 boxes by opening them one by one. Each box you open is one step. This is linear search: up to n steps.
  2. If the boxes are in order, open the middle one and throw away the wrong half. Repeat. Binary search finds 13 in only 4 steps.
  3. Now compare five kinds of algorithm when n = 16. The bar height shows the number of steps. Some stay tiny, one is huge.
  4. Double the input from 8 to 16. O(n) doubles, O(n²) becomes four times bigger, and O(log n) grows by just 1.
  5. Sorting 1000 items: bubble sort needs about a million comparisons, merge sort only about ten thousand. Growth rate matters most for big inputs.
  6. Your turn: move the n slider from 2 to 1024. Watch which bar shoots up fastest.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why not just time the program with a stopwatch?

Time changes from computer to computer. Step counts do not. Step 1 counts boxes opened, not seconds.

Why can binary search skip half the boxes?

The boxes are in order. If the middle is smaller than the target, everything on its left is also smaller, so none of them can be the answer. Watch the grey boxes in step 2.

Why do we drop constants in Big O?

Big O is about how fast the work grows. 2n and n both double when n doubles, so they grow the same way. Step 4 shows the doubling.

Is O(n²) always slower than O(n log n)?

For tiny n it can even be faster, but as n grows, n² overtakes quickly. In step 5 with n = 1000 the gap is about 100 times.

What does log n actually mean here?

log₂ n is how many times you can halve n before reaching 1. For 1024 it is 10. Move the slider in the last step and watch the blue bar grow by only 1 each time n doubles.

Many algorithms for one problem

An algorithm is a set of exact steps to solve a problem. Most problems can be solved by more than one algorithm. For example, to find a name in a list you can check every name, or (if the list is sorted) you can keep halving it.

Both give the right answer. The difference is efficiency: how much work and memory each one needs. A good programmer picks the algorithm that stays fast when the data becomes big.

Comparing algorithms by time taken

Timing with a stopwatch is not fair: a fast computer makes a slow algorithm look good. So we count basic steps (comparisons, swaps, additions) as a function of the input size n.

Best, average and worst case

The best case is the luckiest input (13 is in the first box: 1 step). The worst case is the unluckiest (13 is not there: n steps). We usually quote the worst case, because it is a promise: the algorithm will never be slower than this.

Time complexity and space complexity

Time complexity tells how the number of steps grows with n. Space complexity tells how the extra memory grows with n. Merge sort is fast but needs extra memory; bubble sort needs almost no extra memory but is slow.

Big O notation

Big O describes the growth rate, ignoring small details. We keep only the biggest term and drop constant numbers: 3n² + 5n + 2 becomes O(n²), because for big n the n² part is almost everything.

Big ONamen = 16n = 1000Example
O(1)constant11read item 5 of an array
O(log n)logarithmic4about 10binary search
O(n)linear161000linear search, find the largest
O(n log n)n log n64about 10,000merge sort
O(n²)quadratic2561,000,000bubble sort, nested loops

Quick rule for code: one loop over n items is O(n); a loop inside a loop is O(n²); halving the problem each time is O(log n).

Efficiency of linear and binary search

Linear search checks items one by one. Worst case: n comparisons, so O(n). It works on any list, sorted or not.

Binary search needs a sorted list. Look at the middle; if it is too big, throw away the right half, else the left half. Each step halves the list, so the worst case is about log₂ n + 1 comparisons: O(log n). For 1,000,000 items that is about 20 steps instead of 1,000,000.

Efficiency of sorting algorithms

Bubble, insertion and selection sort use a loop inside a loop, so they take about n²/2 comparisons: O(n²). Insertion sort is O(n) in the best case (an already sorted list).

Merge sort halves the list about log₂ n times and does about n work at each level: O(n log n). It needs O(n) extra memory.

Binary search needs a sorted list. If you search only once, sorting first (n log n) costs more than one linear search (n). If you search many times, sorting once pays off.

Preconditions, postconditions and recursion pitfalls

A precondition is what must be true before the algorithm starts (binary search: the list is sorted). A postcondition is what is promised when it ends (sorting: every item is less than or equal to the next). Writing them down helps you test and prove an algorithm.

Recursion means a function calls itself on a smaller problem. Common mistakes:

Try it: race two searches

Write the numbers 1 to 32 on paper slips and put them in order face down. Ask a friend to pick a secret number. First search one by one and count the slips you turn. Then search by always turning the middle slip. Repeat 5 times. Which method never needed more than 6 turns? Check with the slider in the last 3D step (n = 32: log₂ 32 = 5).

Key formulas and definitions

Worked examples

1. A list has 50 names. How many comparisons does linear search need in the best and worst case?

Best case: the name is first → 1 comparison. Worst case: the name is last or missing → 50 comparisons. Linear search is O(n).

2. What is the most comparisons binary search needs for a sorted list of 1024 items?

Each step halves the list: 1024 → 512 → 256 → 128 → 64 → 32 → 16 → 8 → 4 → 2 → 1. That is 10 halvings, plus the last check: at most 11 comparisons (log₂ 1024 = 10).

3. Give the Big O of f(n) = 4n² + 10n + 7.

Keep the biggest term (4n²) and drop the constant 4: O(n²).

4. A loop runs i from 1 to n, and inside it another loop runs j from 1 to n. How many times does the inner line run?

n times for each of n values of i: n × n = n². Time complexity O(n²).

5. An O(n²) program sorts 1000 items in 2 seconds. Roughly how long for 3000 items?

n becomes 3 times bigger, so n² becomes 3² = 9 times bigger: about 2 × 9 = 18 seconds.

6. Compare bubble sort and merge sort for n = 1000 items.

Bubble sort: about n²/2 = 500,000 comparisons. Merge sort: about n log₂ n = 1000 × 10 = 10,000. Merge sort does about 50 times less work, but needs extra memory.

Common mistakes

Practice quiz

1. What is the worst-case time complexity of linear search?
2. Binary search only works if the list is:
3. If n doubles, an O(n²) algorithm takes about:
4. Which sort is O(n log n)?
5. Big O of 7n + 300 is:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is time complexity in simple words?

It tells how the number of steps an algorithm takes grows when the input gets bigger. For example O(n) means double the input, double the steps.

What is the difference between time and space complexity?

Time complexity measures steps; space complexity measures extra memory. An algorithm can be fast but use a lot of memory, like merge sort.

Which is the fastest Big O?

O(1) (constant) is best, then O(log n), O(n), O(n log n), O(n²), and exponential O(2ⁿ) is the worst of the common ones.

Where this is taught

Canada (Ontario)Grade 12C. Designing Modular Programs
Ukraine11 класAlgorithms
England (GCSE, A level)Year 103.1 Fundamentals of algorithms
USA (Common Core, NGSS, AP)Grade 11Selection and Iteration
USA (Common Core, NGSS, AP)Grade 11Algorithms and Programming
South Korea고등학교 2학년Algorithms and programming
South Korea고등학교 3학년Abstraction and algorithms

Learn first

Learn next

Related lessons

All Computer Science lessons