The analysis of loops for the complexity analysis of algorithms involves finding the number of operations performed by a loop as a function of the input size. This is usually done by determining the number of iterations of the loop and the number of operations performed in each iteration.
Here are the general steps to analyze loops for complexity analysis:
Determine the number of iterations of the loop. This is usually done by analyzing the loop control variables and the loop termination condition.
Determine the number of operations performed in each iteration of the loop. This can include both arithmetic operations and data access operations, such as array accesses or memory accesses.
Express the total number of operations performed by the loop as a function of the input size. This may involve using mathematical expressions or finding a closed-form expression for the number of operations performed by the loop.
Determine the order of growth of the expression for the number of operations performed by the loop. This can be done by using techniques such as big O notation or by finding the dominant term and ignoring lower-order terms.
The time complexity of a function (or set of statements) is considered as O(1) if it doesn’t contain a loop, recursion, and call to any other non-constant time function.
i.e. set of non-recursive and non-loop statements
In computer science, O(1) refers to constant time complexity, which means that the running time of an algorithm remains constant and does not depend on the size of the input. This means that the execution time of an O(1) algorithm will always take the same amount of time regardless of the input size. An example of an O(1) algorithm is accessing an element in an array using an index.
- swap() function has O(1) time complexity.
- A loop or recursion that runs a constant number of times is also considered O(1). For example, the following loop is O(1).
C++
for ( int i = 1; i <= c; i++) {
}
|
C
for ( int i = 1; i <= c; i++) {
}
|
Java
for ( int i = 1 ; i <= c; i++) {
}
|
C#
for ( int i = 1; i <= c; i++) {
}
|
Javascript
for ( var i = 1; i <= c; i++) {
}
|
Python3
The Time Complexity of a loop is considered as O(n) if the loop variables are incremented/decremented by a constant amount. For example following functions have O(n) time complexity. Linear time complexity, denoted as O(n), is a measure of the growth of the running time of an algorithm proportional to the size of the input. In an O(n) algorithm, the running time increases linearly with the size of the input. For example, searching for an element in an unsorted array or iterating through an array and performing a constant amount of work for each element would be O(n) operations. In simple words, for an input of size n, the algorithm takes n steps to complete the operation.
C++
for ( int i = 1; i <= n; i = i + c) {
}
for ( int i = n; i > 0; i = i - c) {
}
|
C
for ( int i = 1; i <= n; i += c) {
}
for ( int i = n; i > 0; i -= c) {
}
|
Java
for ( int i = 1 ; i <= n; i += c) {
}
for ( int i = n; i > 0 ; i -= c) {
}
|
C#
for ( int i = 1; i <= n; i = i + c) {
}
for ( int i = n; i > 0; i = i - c) {
}
|
Javascript
for ( var i = 1; i <= n; i += c) {
}
for ( var i = n; i > 0; i -= c) {
}
|
Python3
for i in range ( 1 , n + 1 , c):
for i in range (n, 0 , - c):
|
The time complexity is defined as an algorithm whose performance is directly proportional to the squared size of the input data, as in nested loops it is equal to the number of times the innermost statement is executed. For example, the following sample loops have O(n2) time complexity
Quadratic time complexity, denoted as O(n^2), refers to an algorithm whose running time increases proportional to the square of the size of the input. In other words, for an input of size n, the algorithm takes n * n steps to complete the operation. An example of an O(n^2) algorithm is a nested loop that iterates over the entire input for each element, performing a constant amount of work for each iteration. This results in a total of n * n iterations, making the running time quadratic in the size of the input.
C++
for ( int i = 1; i <= n; i += c) {
for ( int j = 1; j <= n; j += c) {
}
}
for ( int i = n; i > 0; i -= c) {
for ( int j = i + 1; j <= n; j += c) {
}
}
for ( int i = n; i > 0; i -= c) {
for ( int j = i - 1; j > 0; j -= c) {
}
}
|
C
for ( int i = 1; i <= n; i += c) {
for ( int j = 1; j <= n; j += c) {
}
}
for ( int i = n; i > 0; i -= c) {
for ( int j = i + 1; j <= n; j += c) {
}
}
|
Java
for ( int i = 1 ; i <= n; i += c) {
for ( int j = 1 ; j <= n; j += c) {
}
}
for ( int i = n; i > 0 ; i -= c) {
for ( int j = i + 1 ; j <= n; j += c) {
}
}
|
C#
using System;
class Program {
static void Main()
{
int n = 10;
int c = 2;
for ( int i = 1; i <= n; i += c) {
for ( int j = 1; j <= n; j += c) {
Console.WriteLine( "Expression at (" + i
+ ", " + j + ")" );
}
}
for ( int i = n; i > 0; i -= c) {
for ( int j = i + 1; j <= n; j += c) {
Console.WriteLine( "Expression at (" + i
+ ", " + j + ")" );
}
}
for ( int i = n; i > 0; i -= c) {
for ( int j = i - 1; j > 0; j -= c) {
Console.WriteLine( "Expression at (" + i
+ ", " + j + ")" );
}
}
}
}
|
Javascript
for ( var i = 1; i <= n; i += c) {
for ( var j = 1; j <= n; j += c) {
}
}
for ( var i = n; i > 0; i -= c) {
for ( var j = i + 1; j <= n; j += c) {
}
}
|
Python3
for i in range ( 1 , n + 1 , c):
for j in range ( 1 , n + 1 , c):
for i in range (n, 0 , - c):
for j in range (i + 1 , n + 1 , c):
|
The time Complexity of a loop is considered as O(Logn) if the loop variables are divided/multiplied by a constant amount. And also for recursive calls in the recursive function, the Time Complexity is considered as O(Logn).
C++
for ( int i = 1; i <= n; i *= c) {
}
for ( int i = n; i > 0; i /= c) {
}
|
C
for ( int i = 1; i <= n; i *= c) {
}
for ( int i = n; i > 0; i /= c) {
}
|
Java
for ( int i = 1 ; i <= n; i *= c) {
}
for ( int i = n; i > 0 ; i /= c) {
}
|
C#
using System;
class Program {
static void Main( string [] args)
{
int n = 10;
int c = 2;
for ( int i = 1; i <= n; i *= c) {
Console.WriteLine( "i = " + i);
}
for ( int i = n; i > 0; i /= c) {
Console.WriteLine( "i = " + i);
}
}
}
|
Javascript
for ( var i = 1; i <= n; i *= c) {
}
for ( var i = n; i > 0; i /= c) {
}
|
Python3
i = 1
while (i < = n):
i = i * c
i = n
while (i > 0 ):
i = i / / c
|
C++
void recurse( int n)
{
if (n <= 0)
return ;
else {
}
recurse(n/c);
}
|
C
void recurse( int n)
{
if (n <= 0)
return ;
else {
}
recurse(n/c);
}
|
Java
void recurse( int n)
{
if (n <= 0 )
return ;
else {
}
recurse(n/c);
}
|
C#
using System;
class Program {
static void Recurse( int n, int c)
{
if (n <= 0)
return ;
else {
Recurse(n / c, c);
}
}
static void Main()
{
int n = 10;
int c = 2;
Recurse(n, c);
Console.WriteLine( "Recursive function executed." );
}
}
|
Javascript
function recurse(n)
{
if (n <= 0)
return ;
else {
}
recurse(n/c);
}
|
Python3
def recurse(n):
if (n < = 0 ):
return
else :
recurse(n / c)
|
The Time Complexity of a loop is considered as O(LogLogn) if the loop variables are reduced/increased exponentially by a constant amount.
C++
for ( int i = 2; i <= n; i = pow (i, c)) {
}
for ( int i = n; i > 1; i = fun(i)) {
}
|
C
for ( int i = 2; i <= n; i = pow (i, c)) {
}
for ( int i = n; i > 1; i = fun(i)) {
}
|
Java
for ( int i = 2 ; i <= n; i = Math.pow(i, c)) {
}
for ( int i = n; i > 1 ; i = fun(i)) {
}
|
C#
using System;
public class Main
{
public static void Execute( string [] args)
{
int n = 100;
int c = 2;
for ( int i = 2; i <= n; i = ( int )Math.Pow(i, c))
{
Console.WriteLine(i);
}
for ( int i = n; i > 1; i = fun(i))
{
Console.WriteLine(i);
}
}
public static int fun( int num)
{
return ( int )Math.Sqrt(num);
}
}
|
Javascript
for ( var i = 2; i <= n; i = i**c) {
}
for ( var i = n; i > 1; i = fun(i)) {
}
|
Python3
i = 2
while (i < = n):
i = i * * c
i = n
while (i > 1 ):
i = fun(i)
|
See this for mathematical details.
When there are consecutive loops, we calculate time complexity as a sum of the time complexities of individual loops.
To combine the time complexities of consecutive loops, you need to consider the number of iterations performed by each loop and the amount of work performed in each iteration. The total time complexity of the algorithm can be calculated by multiplying the number of iterations of each loop by the time complexity of each iteration and taking the maximum of all possible combinations.
For example, consider the following code:
for i in range(n):
for j in range(m):
# some constant time operation
Here, the outer loop performs n iterations, and the inner loop performs m iterations for each iteration of the outer loop. So, the total number of iterations performed by the inner loop is n * m, and the total time complexity is O(n * m).
In another example, consider the following code:
for i in range(n):
for j in range(i):
# some constant time operation
Here, the outer loop performs n iterations, and the inner loop performs i iterations for each iteration of the outer loop, where i is the current iteration count of the outer loop. The total number of iterations performed by the inner loop can be calculated by summing the number of iterations performed in each iteration of the outer loop, which is given by the formula sum(i) from i=1 to n, which is equal to n * (n + 1) / 2. Hence, the total time complex
C++
for ( int i = 1; i <= m; i += c) {
}
for ( int i = 1; i <= n; i += c) {
}
|
C
for ( int i = 1; i <= m; i += c) {
}
for ( int i = 1; i <= n; i += c) {
}
|
Java
for ( int i = 1 ; i <= m; i += c) {
}
for ( int i = 1 ; i <= n; i += c) {
}
|
C#
for ( int i = 1; i <= m; i += c)
{
}
for ( int i = 1; i <= n; i += c)
{
}
|
Javascript
for ( var i = 1; i <= m; i += c) {
}
for ( var i = 1; i <= n; i += c) {
}
|
Python3
for i in range ( 1 , m + 1 , c):
for i in range ( 1 , n + 1 , c):
|
As discussed here, the worst-case time complexity is the most useful among best, average and worst. Therefore we need to consider the worst case. We evaluate the situation when values in if-else conditions cause a maximum number of statements to be executed.
For example, consider the linear search function where we consider the case when an element is present at the end or not present at all.
When the code is too complex to consider all if-else cases, we can get an upper bound by ignoring if-else and other complex control statements.
The time complexity of a recursive function can be written as a mathematical recurrence relation. To calculate time complexity, we must know how to solve recurrences. We will soon be discussing recurrence-solving techniques as a separate post.
Algorithms Cheat Sheet:
Algorithm |
Best Case |
Average Case |
Worst Case |
Selection Sort |
O(n^2) |
O(n^2) |
O(n^2) |
Bubble Sort |
O(n) |
O(n^2) |
O(n^2) |
Insertion Sort |
O(n) |
O(n^2) |
O(n^2) |
Tree Sort |
O(nlogn) |
O(nlogn) |
O(n^2) |
Radix Sort |
O(dn) |
O(dn) |
O(dn) |
Merge Sort |
O(nlogn) |
O(nlogn) |
O(nlogn) |
Heap Sort |
O(nlogn) |
O(nlogn) |
O(nlogn) |
Quick Sort |
O(nlogn) |
O(nlogn) |
O(n^2) |
Bucket Sort |
O(n+k) |
O(n+k) |
O(n^2) |
Counting Sort |
O(n+k) |
O(n+k) |
O(n+k) |
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Contact Us