A-level Computing/AQA/Paper 1/Theory of computation/Maths for Big-O Notation






 * Linear time, polynomial time, exponential time.
 * Order of complexity.

Big O Notation (also known as Big-O Notation) is a mathematical way of describing the limiting behaviours of a function. In other words, it is a way of defining how efficient an algorithm is by how "fast" it will run.

Timing
You can work out the time that an algorithm takes to run by timing it:

However, this isn't always suitable. What happens if you run some code on a 33 MHz processor, and the same code on a 3.4 GHz processor? Timing a function tells you a lot about the speed of a computer, and very little about the speed of an algorithm.

Refining algorithms
We might want to write a program to calculate the sum of all the numbers between 0 and a variable, where   in this case == 7483647. To solve this, you might write a solution that has a loop that cycles through each number all the way up to, adding it to another variable. The code might look a little like thisː

It certainly works, but as  gets larger the program runs more slowly. In fact, there is a relationship between the value of  and the speed of the algorithm. If you were to double, the algorithm would take twice as long and if you were to treble  , the code would take three times as long. We can describe the speed of this relationship using big O notationː $$O(n)\,$$. That is the speed of the algorithm is related to the number of items being processed, in our case. This is known as linear time.

We can of course write this is a much faster wayː

This version of the algorithm gets the same result, but doesn't use a loop. It completes in the same amount of time whatever value of  you give it. We can describe this algorithm using big O notation as beingː $$O(1)\,$$. That is, it completes in a constant time.