Uploaded by kudzaisheranganayi

19.1 Big O Notation

advertisement
Page | 3
Big O Notation
Big O notation is a way to calculate efficiency of an algorithm.
Big O notation is used to classify algorithms according to how their running time (or space requirements)
grows as the input size grows.
The letter O is used because the growth rate of a function is also referred to as ‘order of the function’.
The worst-case scenario is used when calculating the order of growth for very large data sets.
Consider the linear search algorithm, the worst-case scenario is that the item searched for is the last item in
the list. The longer the list, the more comparisons have to be made. If the list is twice as long, twice as many
comparisons have to be made. Generally, we can say the order of growth is linear. We write this as O(n),
where n is the size of the data set. Consider the bubble sort algorithm for the worst-case scenario.
Unsorted ← n – 1
FOR i ← 0 TO n – 2
FOR j ← 0 TO Unsorted – 1
IF MyList[j] > MyList[j + 1] THEN
Temp ← MyList[j]
MyList[j] ← MyList[j + 1]
MyList[j + 1] ← Temp
ENDIF
NEXT j
Unsorted ← Unsorted - 1
NEXT i
The basic operation for this algorithm is the comparison IF MyList[j] > MyList[j + 1]
N
Number of comparisons
1
0
2
1
3
3
=1+2
4
6
=1+2+3
5
10
=1+2+3+4
6
15
=1+2+3+4+5
We can see that the total number of comparisons is the sum of the first (n – 1) whole numbers. This leads us
to the formula:
½ * n * (n – 1) = ½ * (n2 – n)
Big O notation on linear and binary search.
For linear Search Big O notation is O(N) as its growth is linear, double the number of items, double the
comparisons taken place. While in binary search, with each iteration this algorithm halves the number of
values in the data set. This iterative halving of data sets produces a growth curve that peaks at the beginning
and slowly flattens out as the size of the data sets increase. This type of algorithm is described as O(log2n)
Page | 4
Big O order of time complexity
Description
describes an algorithm that always takes the same
O(1)
time to perform the task
describes an algorithm where the time to perform
O(N)
the task will grow linearly in direct proportion to N,
the number of items of data the algorithm is using
describes an algorithm where the time to perform
the task will grow linearly in direct proportion to the
O(N2)
square of N, the number of items of data the
algorithm is using
describes an algorithm where the time to perform
O(2N)
the task doubles every time the algorithm uses an
extra item of data
describes an algorithm where the time to perform
O(LogN) the task goes up linearly as the number of items
goes up exponentially
Big O order of space complexity
Description
describes an algorithm that always uses the same
O(1)
space to perform the task
O(N)
describes an algorithm where the space to perform
the task will grow linearly in direct proportion to N,
the number of items of data the algorithm is using
Example
deciding if a number is even or odd
a linear search
bubble sort, insertion sort
calculation of Fibonacci numbers
using recursion
binary search
Example
any algorithm that just uses variables,
for example d = a + b + c
any algorithm that uses arrays, for
example a loop to calculate a running
total of values input to an array of N
elements
4ac 9618 W22 P31
12 (b) Big O notation is used to classify efficiency of algorithms.
The Big O notation for time complexity in a binary search is O(log n).
(i) State the Big O notation for time complexity of a linear search.
..................................................................................................................................... [1]
(ii) Describe the meaning of O(log n) as it applies to a binary search algorithm.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
..................................................................................................................................... [2]
12(b)(i)
O(n)
1
12(b)(ii)
One mark for each point (Max 2)
•
O(log n) is a time complexity that uses logarithmic time.
•
The time taken goes up linearly as the number of items rises exponentially
•
O(log n) is the worst case scenario (time complexity for a binary search).
2
Download