0% found this document useful (0 votes)
18 views

Time and Space Complexity

The document discusses performance analysis of algorithms, focusing on space and time complexity. Space complexity includes fixed and variable parts, while time complexity is influenced by factors like machine type and input data. Various types of time complexity are outlined, including constant, linear, logarithmic, and quadratic time complexities, with examples provided for each type.

Uploaded by

srinu vas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Time and Space Complexity

The document discusses performance analysis of algorithms, focusing on space and time complexity. Space complexity includes fixed and variable parts, while time complexity is influenced by factors like machine type and input data. Various types of time complexity are outlined, including constant, linear, logarithmic, and quadratic time complexities, with examples provided for each type.

Uploaded by

srinu vas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Performance Analysis

• Performance analysis of an algorithm is the process of calculating space required by that algorithm
and time required by that algorithm.

– Space Complexity: Space required to complete the task of that algorithm

• includes program space

• dataspace

– Time Complexity: Time required to complete the task of that algorithm (Time Complexity

Space Complexity
 Space complexity of an algorithm represents the amount of memory space required by the
algorithm in its life cycle.
 The space required by an algorithm is equal to the sum of the following two components −
1. A fixed part that is a space required to store certain data and variables, that are
independent of the size of the problem. For example, simple variables and constants
used, program size, etc.
2. A variable part is a space required by variables, whose size depends on the size of the
problem. For example, dynamic memory allocation, recursion stack space, etc.

Space complexity S(P) of any algorithm P is S(P) = C + SP(I), where C is the fixed part and S(I) is the
variable part of the algorithm, which depends on instance characteristic I. Following is a simple
example that tries to explain the

concept −

Algorithm: SUM(A, B)

Step 1 - START

Step 2 - C ← A + B + 10

Step 3 - Stop

Here we have three variables A, B, and C and one constant. Hence S(P) = 1 +3. Now, space depends
on data types of given variables and constant types and it will be multiplied accordingly.

1.Constant Space (O(1) ):

 Algorithms with constant space complexity use a fixed amount of memory that does not
depend on the input size.

2. Linear Space (O(n)):

 Linear space complexity implies that the memory usage grows linearly with the size of the
input.
Time Complexity

The time complexity of an algorithm is the total amount of time required by an


algorithm to complete its execution.
Running time of an algorithm depends upon the following...
– Whether it is running on Single processor machine or Multi processor
machine.
– Whether it is a 32 bit machine or 64 bit machine
– Read and Write speed of the machine.
– The time it takes to perform Arithmetic operations, logical operations, return
value and assignment operations etc.,
– Input data

When we calculate time complexity of an algorithm, we consider only input data and ignore
the remaining things, as they are machine dependent

Constant Time Complexity


If any program requires fixed amount of time for all input values, then its time complexity is
said to be Constant Time Complexity.

int
1 unit of time to calculate a+b .
sum(int a, int b)
{ return a+b; 1 unit of time to return the value.
}
Total 2 units of time to complete its execution

Linear Time Complexity


If the amount of time required by an algorithm is increased with the increase of input
value, then that time complexity is said to be Linear Time Complexity
Different types of time complexity:
Constant time – O (1)

An algorithm is said to have constant time with order O (1) when it is not dependent on the input
size n. Irrespective of the input size n, the runtime will always be the same.

Example: Int 1=1;

While(i<=5)

Printf(“%d”,i);

++i;

Linear time – O(n)

An algorithm is said to have a linear time complexity when the running time increases linearly with
the length of the input. When the function involves checking all the values in input data, with this
order O(n).
Example: Int 1=1;

While(i<=N)

Printf(“%d”,i);

++i;

Logarithmic time – O (log n)

An algorithm is said to have a logarithmic time complexity when it reduces the size of the input data
in each step. This indicates that the number of operations is not the same as the input size. The
number of operations gets reduced as the input size increases. Algorithms are found in binary
trees or binary search functions.

Example: Int 1=1;

While(i<=N)

Printf(“%d”,i);

i=i*3;

Quadratic time – O (n^2)

An algorithm is said to have a non-linear time complexity where the running time increases non-
linearly (n^2) with the length of the input. Generally, nested loops come under this order where one
loop takes O(n) and if the function involves a loop within a loop, then it goes for O(n)*O(n) = O(n^2)
order.
Example: Int 1=1;

While(i<=N)

For(int j=1;j<=N;++j)

Printf(“%d”,i);

++i;

You might also like