0% found this document useful (0 votes)
9 views5 pages

Complexity New

Computational complexity measures the performance of algorithms based on time and space requirements. It includes time complexity, which quantifies the number of steps based on input size, and space complexity, which assesses memory usage. The document also discusses Big O notation, types of complexities, and compares various algorithms to determine their efficiency.

Uploaded by

sivadaachu0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Complexity New

Computational complexity measures the performance of algorithms based on time and space requirements. It includes time complexity, which quantifies the number of steps based on input size, and space complexity, which assesses memory usage. The document also discusses Big O notation, types of complexities, and compares various algorithms to determine their efficiency.

Uploaded by

sivadaachu0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Computational complexity

Computational complexity refers to the growth rate or measure of the performance of an


algorithm. The level in difficulty in solving mathematically posed problems are measured by the
time required, number of steps involved in solving or memory space require.
Measuring the efficiency of an algorithm
Efficiency of an algorithm depends on two factors

• internal factors
time required to run a program, space or memory required
• external factors
size of input to the algorithm, speed of the computer on which it is run, quality of the
compiler

External factors can be controlled by the user. Usually efficiency is measured in terms of
space and time only.
Types of complexity
The two factors which specify the complexity or efficiency of an algorithm are
• time complexity
• space complexity
Time complexity
It is defined as the number of steps taken to solve an instance of the problem as a function of the
size of the input. it is a concept that measure the time required to run an algorithm. An algorithm
which takes the least number of steps is considered as the most efficient.
Space complexity
It is a concept that measures the amount of space or memory required by an algorithm. the space
complexity is measured with big O notation which is used to determine the number of steps
involved in an algorithm for solving a problem.
Three cases to analyze an algorithm
Three cases to state the degree of performance of an algorithm are

• best case complexity


It means that the function takes the minimum time or uses minimum number of steps on
an algorithm of size N
• worst case complexity
it means that the function takes the maximum time or uses maximum number of steps on
an algorithm of size N
example searching for an element which is not present in an array in linear search
• Average case complexity it means that the function takes the average time or uses average
number of steps on an algorithm of size N

1
Big O Notation
It is the unit of measurement of an algorithm.
Big O Notation is used to determine the number of steps involved in an algorithm for
solving problem. The number of steps depends on the size of the input data.

Dominant term
The dominant term is the term that grows the fastest. It plays a vital role and affects the
performance of the function .
Example
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
…………
}}
for (int j=1;j<=n;j++)
{
…………
}
Complexity = O(n2) +O(n)
Here O(n2) is the dominant term while calculating the complexity

Constants
In determining Big O complexity, lower order terms or low priority terms and constants
are dropped and not required. But even if two algorithms have same Big O complexity,
one may be faster than the other. Constants helps to find out the performance of
algorithms when they have the same Big O notations.

Types of time complexity cases


• constant time complexity - O(1)
• Logarithmic time complexity – O( log n)
• Linear time complexity - O(n)
• quadratic time complexity - O(n2)
• exponential time complexity - O(2n)
• Linearithmetic time complexity O(n log n)

Calculating complexity
• Simple statements or group of simple statements O(1)
They run in constant time
int a;
a=5;
++a;
Complexity = O(1)

2
• Loops (single loop) -O(N)
Running time of single loop=running time of statements in it * number of iterations
Eg for (int i=1;i<=n;i++)
{
m=m+2;
}
Total time = c*n
=cn
=n
O(n)
• Nested loop - O(N2)
Running time of nested loop=running time of statements in it * product of the sizes of all
loops.
Ex for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
m=m+2;
}}

Total time = c*n*n

=cn 2

=n

O(n 2)
3 loops =O(n 3 )
4 loops = O(n 4)

• Consecutive statements
Running time= Add the time complexities of each statement
x=x+1;
for (int i=1;i<=n;i++)
{
p=p*2;
}
for (int j=1;j<=n;j++)
{
m=m+2;
}
Total time = c0+ c1*n+ c2*n
c0+ n(c1+c2)

3
= O(n)
• Simple if else statement = O(1)
• Nested if else statement - O(n)
• If else statement with loops =O(n2)

Write the time complexities for the following


Linear search - O(n)
Binary search - O( log n).
Bubble sort - O(n 2 )
Selection sort - O(n 2 )
Insertion sort - O(n 2 )
Merge sort - O(n log n)
Quick sort - O(n log n)
Recursive algorithm - O(2n)
Speed Comparison (From fastest to slowest)
O(1) <O(Log N) < O(N) < O(N log N) < O(N2) < O(N3) < O(2n)

Name the fastest searching algorithm?


Binary search
Name the fastest sorting algorithm?
Quick and Merge sort

Compare the two complexities O(n 2 )and O(2 n )and state which is better and why?
O(n2) is better than O( 2 n)
Because if n=10, then n 2= 100
If 2 n= 2 10= 1024. So 100 times will finish fast. Therefore the complexity in
O(n2) is better than O( 2 n).

4
5

You might also like