Big O
Big O
So, what do we mean by faster? About the only measure of speed we can apply to an algorithm
is to count the number of operations that it has to perform to produce its result. The fewer the
number of operations, the faster it will execute. This obviously has a relationship with the time
that it will take to execute, but so do a lot of other factors such as processor speed, amount of
memory available etc. Therefore any attempt to give an absolute measure of time would be
pretty much meaningless.
There are three ways of expressing performance: Best-case, worst-case and average case.
Consider the task of performing a sequential search on some sort of list, e.g. an array. Best-case
would be that your target value was found in the first element. Worst-case would be that the
value was not there at all (so all elements would have to be compared and tested, including the
last). Average-case would be mid-way between the two.
Of course, some operations would remain constant, regardless of the size of the list: write
myarray[n]; would always execute in one move, no matter how many elements there were.
Big O notation.
We have already seen that efficiency is defined as the number of operations an algorithm has to
perform to achieve its result. Big O notation is simply a convenient theoretical way of measuring
the execution of an algorithm, therefore expressing its efficiency.
A Big O expression always presents the worst-case scenario. (By the way, the “O” is really the
upper case Greek letter omicron, but this looks just like the letter O!)
Big O notation says that the computing time of an algorithm grows no faster (as Big O presents
a worst-case scenario) than a constant multiplied by f(n). The constant is machine specific (as we
mentioned earlier it depends on such factors as processor speed) so is not usually included in a
generalised Big O expression. Big O therefore is most useful as a comparative measure between
algorithms rather than an absolute measure.
Linear search has a time efficiency (an order) of O(n), where n is the number of elements being
searched. If the list doubles in size, the time taken also doubles. A bubble sort is determined to
be of order O(n2) so the time taken quadruples if n doubles.
Here is a table of the relevant functions for different types of algorithm, expressed in order of
efficiency from the fastest to the slowest:
O (n log2n) Quicksort
O (n3) Cubic Not examined at HL IB. Included only for interest and
completeness!
O (mn)** Exponential
O (n!) Factorial
n is the number of items that must be handled by the algorithm.
* Statements which always ‘hit the mark’ first time have this notation. A further example would be a
perfect hashing algorithm, i.e. one that will always hash to the correct location without needing any
overflow structure.
(a) Given the subalgorithm below, state the exact number of statements in terms of N that
will be executed by this subalgorithm. Describe how the answer is derived.
Assume array A is an array of integers and the number of entries in A is N. I, J and T are
integers.
TEST
I <-- 0
While I <= N do
J <-- 0
T <-- 0
While J <= N do
T <-- T + A(J)
J <-- J + 1
enddo
A(I) <-- T
I <-- I + 1
enddo
end TEST
(b) The following questions relate to methods of searching. Briefly explain your reasoning in
each case.
(i) Of the two search methods, binary and sequential, which one is usually the faster when
the file is short? Which one is usually the faster when the file is long? [2 marks]
(ii) Which of these two methods requires the list to be sorted? [2 marks]
(iii) When performing a sequential search on a list of size N, for the worst possible case, what
is the number of items that must be compared to locate the desired item?
[3 marks]
(iv) When performing a sequential search on a list of size N, for the average case, what is the
number of items that must be compared to locate the desired item?
[3 marks]
(v) When performing a binary search on a list of size N, for the worst possible case, what is
the approximate number of items that must be compared to locate the desired item? [3 marks]
(see also May ’97 paper 1 question 5 and May ’98 paper 2 question 1)
big-O notation
(definition)
Definition: A theoretical measure of the execution of an algorithm, usually the time or memory needed, given
the problem size n, which is usually the number of items. Informally, saying some equation f(n) = O(g(n))
means it is less than some constant multiple of g(n). The notation is read, "f of n is big oh of g of n".
Formal Definition: f(n) = O(g(n)) means there are positive constants c and k, such that 0 f(n) cg(n) for all
n k. The values of c and k must be fixed for the function f and must not depend on n.
Also known as O.
See also (n), (n), (n), , little-o notation, asymptotic upper bound, asymptotically tight bound, NP,
complexity, model of computation.
Note: As an example, n2 + 3n + 4 is O(n2), since n2 + 3n + 4 < 2n2 for all n > 10. Strictly speaking, 3n + 4 is
O(n2), too, but big-O notation is often misused to mean equal to rather than less than. The notion of "equal to"
is expressed by (n).
The importance of this measure can be seen in trying to decide whether an algorithm is adequate, but may just
need a better implementation, or the algorithm will always be too slow on a big enough input. For instance,
quicksort, which is O(n log n) on average, running on a small desktop computer can beat bubble sort, which is
O(n2), running on a supercomputer if there are a lot of numbers to sort. To sort 1,000,000 numbers, the
quicksort takes 20,000,000 steps on average, while the bubble sort takes 1,000,000,000,000 steps!
Any measure of execution must implicitly or explicitly refer to some computation model. Usually this is some
notion of the limiting factor. For one problem or machine, the number of floating point multiplications may be
the limiting factor, while for another, it may be the number of messages passed across a network. Other
measures which may be important are compares, item moves, disk accesses, memory used, or elapsed ("wall
clock") time.
Strictly, the character is the upper-case Greek letter Omicron, not the letter O, but who can tell the difference?
Author: PEB
More information
A rough guide to big-oh notation by Mark Dunlop. Tutorial on complexity classes illustrated with several sort
algorithms.
STUDENT OUTLINE
Lesson 20: Order of Algorithms
The two criteria used for selecting a data structure and algorithm are
INTRODUCTION: the amount of memory required and the speed of execution. The
analysis of the speed of an algorithm leads to a summary statement
called the order of an algorithm.
A. Order of Algorithms
B. Constant Algorithms, O(1)
C. Log2N Algorithms, O(log2N)
D. Linear Algorithms, O(N)
E. N * Log2N Algorithms, O(N * log2N)
F. Quadratic Algorithms, (N2)
G. Other Orders
H. Comparison of Orders of Algorithms
ORDER OF ALGORITHM
VOCABULARY: LOG2 N CONSTANT
N LOG2 N LINEAR
CUBIC QUADRATIC
BIG O NOTATION
DISCUSSION:
A. Order of Algorithms
1. The order of an algorithm is based on the number of steps that it
takes to complete a task. Time is not a valid measuring stick because
computers have different processing speeds. We want a method of
comparing algorithms that is independent of computing environment
and microprocessor speeds.
2. The number of data in the array could vary from 0..4000, but this
does not affect the algorithm of howBig. It will take one step
regardless of how big the data set is.
3. A constant time algorithm could have more than just one step, as
long as the number of steps is independent of the size (N) of the data
set.
4. For example, the number of times the inner loop happens varies
from 1 to (N-1). On average, the inner loop occurs (N/2) times.
G. Other Orders
1. A cubic algorithm is one where the number of steps increases as a
cube of N, or N^3.