You are on page 1of 14

Introduction to Big O Notation

ANALYSIS OF ALGORITHMS

RAZA ABBAS
13K-2023
M. UMER JAVED
13K-2161
AKHLAAK AHMED KHAN
13K-2017

Big O Notation
Big O Notation is a mathematical tool which tells us

the nature of the function. It actually gives us the


estimation between the domain and range of a
function.
For Example: f(x) = O(x4)

Algorithms Time and Memory Consumption


In the field of Computer Science, some algorithms

take more time and memory to execute while others


take less, and one with less time and with less
memory consumption is considered to be more
efficient.
So there comes the need of calculating the time and
memory consumed by the algorithms in order to
compare and choose the most efficient among them
and this helps in developing better algorithms.

Algorithms Time and Space


Everything has its own unit for example: inch is a

unit of length and is less than another unit meter, the


same case is of Big O Notation.
Some algorithms take more time but no extra space
while others take extra space and less time, so the
best algorithm is chosen according to the scenario by
using Big O Notation.
Time and Space of the algorithm depends upon the
input given to the program.

Experimental Approach
There may be the experimental approach of

calculating the time of given algorithm.


We couldve used functions in our programs like
System.currentTime.Millis();
If we put this line at the start and end of the
algorithm and find their difference then we can find
the time taken.
We could run the program with different input sizes
and plot the graph to get the formula.
But. It has some limitations.

Example of Experimentation
Lets take an example of printing n numbers.
for(int i = 0; i < n; i++) {

System.out.print(i);
}
The input given is one million.
On one computer, it took 1.2 seconds while on other, it
took 2 seconds...

Time in MS
100000
90000
80000
70000
60000
PC 1
PC 2

50000
40000
30000
20000
10000
0

10000

20000

30000

40000

50000

60000

Limitations of the Experimental Approach


The time will be defected if we have the input in our

program because the time will be calculated while


the input is taken from the user.
Same hardware and software must be used to
compare two algorithms.
There are interrupts going on in our computer, so
the same algorithm will not give same time for the
same input.

Theoretical Approach
Here comes the need of Big O Notation. The Big O

Notation actually approximates the behavior of the


function in general and gives the approximation of
time and space used by the algorithm(in Computer
Science)
Any input in this case will be taken as n, so it
generalizes the behavior of algorithm.
This approach is free from the dependency on
hardware and software.
Free from the delays in the algorithm.

Example
Again take the same example.
for(int i = 0; i < n; i++) {

System.out.print(i);
}
Lets say, that comparison and assignments take one unit of
time, and printing also takes one unit.
In our case, the whole algorithm took 1+n+1+n+n.
Hence, 3n+2.
We can approximate this as n, because constants doesnt
make any big difference in our time.
So the Big O Notation of our algorithm is O(n).

Slight difference
10,000

1,000

100

10

3n
2n
+
10
n

1
1

10

100

1,000

Growth Rate of an Algorithm


The time taken by each algorithm may vary at

different conditions but the growth rate remains


always same.
This is the main reason that the Big O Notation is
introduced, so that the algorithms can be compared
by their growth rates.
The algorithm with lesser growth rate is more
efficient.

Most Common Functions


n

logn

nlogn

n2

n3

2n

16

64

16

24

64

512

256

16

16

64

256

4,096

65,536

32

32

160

1,024

32,768

4,294,967,296

64

64

384

4,094

262,144

1.84 * 1019

128

128

896

16,384

2,097,152

3.40 * 1038

256

256

2,048

65,536

16,777,216

1.15 * 1077

512

512

4,608

262,144

134,217,728

1.34 * 10154

1024

10

1,024

10,240

1,048,576

1,073,741,824

1.79 * 10308

You might also like