The Order of an Algorithm: Counting Comparisons and Predicting Run Times | Edexcel A Level Further Maths notes

Free preview: The Order of an Algorithm: Counting Comparisons and Predicting Run Times

How Fast Does the Work Grow?

Year 1 (AS) for OCR B (MEI). For Edexcel and OCR A, check your own specification for which parts are AS and which are A level.

Let's start with a question every programmer asks: if my program sorts 1000 names in 1 second, how long will it take for a million names? 1000 seconds? Much longer? The answer depends on the shape of the growth, and that is what this note is about.

The words we need

  • The size of a problem, n, is how much data it has: the number of items to sort, bins to fill or vertices in a network.
  • The efficiency of an algorithm is how the amount of work grows as n grows.
  • We measure work by counting the main operations, usually comparisons (and sometimes swaps or additions).
  • We usually count the worst case: the input that makes the algorithm do the most work. Then we know it can never be slower than our count.

Order: keep only the biggest power

You met bubble sort in an earlier note. On n items it needs at most this many comparisons:

12n(n−1)=12n2−12n

n12n212nTotal comparisons
1050545
1005000504950
1000500 000500499 500
  • When n is large, the n2 term swamps the rest: for n = 1000 it is 500 000 against 500.
  • Constants like 12 depend on the computer and the details, so we ignore them too.
  • So we say bubble sort has quadratic order, or order n2, written O(n2).

If the number of operations is a polynomial in n of degree k, the algorithm has order nk, written O(nk)

💡 In plain words

  • The order tells you the shape of the growth, not the exact time.
  • Order n (linear): double the data, double the time. Order n2 (quadratic): double the data, four times the time. Order n3 (cubic): eight times.

Graph of steps against n for order n (straight line), order n squared and order n cubed. Points (2, 4) and (4, 16) are marked on the order n squared curve

Following the graph

  • All three curves start together at small n. The differences only matter for large problems.
  • On the order n2 curve, going from n = 2 to n = 4 (double) takes the steps from 4 to 16 (four times).
  • The cubic curve shoots up fastest. A cubic algorithm can be useless for big n even on a fast computer.
  • The linear curve grows steadily: it is the cheapest of the three.

✏️ YOUR TURN 1

Try this first:

State the order of an algorithm that needs this many operations in the worst case.

  1. 3n2+5n+2
  2. 4n+7
  3. 16n(n+1)(n+2)

Answer:

  1. Highest power n2: quadratic, O(n2). The 3 is a constant, so it is ignored.
  2. Highest power n: linear, O(n).
  3. Expanding gives 16n3+12n2+13n: cubic, O(n3). You only need to see that three brackets each containing n multiply to make n3.

Finding the Order by Counting

The recipe

  1. Decide what counts as one operation (usually a comparison). Why: we need one thing to count, and comparisons are where the time goes.
  2. Find the worst case: the input that makes the algorithm do the most work. Why: then the order is a guarantee, not a hope.
  3. Write the number of operations as an expression in n, often a sum such as 1+2+…+(n−1). Why: a sum is easier to write down than to see in your head.
  4. Simplify, then keep the highest power of n. Why: that is the term that matters for big n.

💡 The sum you'll use most

1+2+…+(n−1)=12n(n−1). This is the arithmetic series formula from A Level Maths: n−1 terms, average value 12n.

Four examples

  • Finding the largest of n numbers by checking each against the biggest so far: n−1 comparisons. Order n.
  • First fit bin packing: the kth item may be checked against each of the k−1 bins already open. Worst case 0+1+…+(n−1)=12n(n−1). Order n2.
  • First fit decreasing: a bubble sort (order n2) then first fit (order n2). Adding two quadratics gives a quadratic, so order n2.
  • Comparing every pair from a list of n items (for i = 1 to n, for j = 1 to n): n×n=n2 comparisons, order n2. A loop inside a loop usually means quadratic.

Unlock the full note: start 7 days free.

Log in or subscribe to read the rest of this note. Members also get the AI tutor, practice quizzes, mock exams and flashcards for this topic.

Log in · Subscribe · Start free trial

All notes in Algorithms, Complexity, Sorting and Bin Packing

  1. What Is an Algorithm? Flow Charts, Pseudocode and Trace Tables
  2. Bubble Sort, Shuttle Sort and Quick Sort Step by Step
  3. Bin Packing: Lower Bounds, First Fit, First Fit Decreasing and Full Bins
  4. The Order of an Algorithm: Counting Comparisons and Predicting Run Times

What's inside for this topic

4 teacher notes, 83 practice questions, 52 flashcards, 2 quizzes.

Get the full notes, every practice question, flashcards, quizzes and mock papers.

Start your free 7-day trial

Questions? Chat with us on WhatsApp · Try one live lesson

Previous note: Bin Packing: Lower Bounds, First Fit, First Fit Decreasing and Full Bins

The Order of an Algorithm: Counting Comparisons and Predicting Run Times on other exam boards

All Edexcel A Level Further Maths topics · A Level Further Maths