How to Analyse Loops for Complexity Analysis of Algorithms?

Constant Time Complexity O(1):

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.

Linear Time Complexity O(n):

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.

Quadratic Time Complexity O(nc):

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.

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).

Logarithmic Time Complexity O(Log Log n):

The Time Complexity of a loop is considered as O(LogLogn) if the loop variables are reduced/increased exponentially by a constant amount. 

Asymptotic Analysis of Algorithms Notes for GATE Exam [2024]Asymptotic Notations

This Asymptotic Analysis of Algorithms is a critical topic for the GATE (Graduate Aptitude Test in Engineering) exam, especially for candidates in computer science and related fields. This set of notes provides an in-depth understanding of how algorithms behave as input sizes grow and is fundamental for assessing their efficiency. Let’s delve into an introduction for these notes:

Table of Content

  • Introduction of Algorithms
  • Asymptotic Analysis
  • Worst, Best and Average Case
  • How to Analyse Loops for Complexity Analysis of Algorithms?
  • How to combine the time complexities of consecutive loops? 
  • Algorithms Cheat Sheet for Complexity Analysis:
  • Runtime Analysis of Algorithms:
  • Little o and Little omega notations
  • What does ‘Space Complexity’ mean?
  • Previous Year GATE Questions

Similar Reads

Introduction of Algorithms

The word Algorithm means “A set of rules to be followed in calculations or other problem-solving operations” Or “A procedure for solving a mathematical problem in a finite number of steps that frequently involves recursive operations “....

Asymptotic Analysis

Given two algorithms for a task, how do we find out which one is better?...

Measurement of Complexity of an Algorithm (Worst, Best and Average Case)

Based on the above three notations of Time Complexity there are three cases to analyze an algorithm:...

How to Analyse Loops for Complexity Analysis of Algorithms?

Constant Time Complexity O(1):...

How to combine the time complexities of consecutive loops?

When there are consecutive loops, we calculate time complexity as a sum of the time complexities of individual loops....

Algorithms Cheat Sheet for Complexity Analysis:

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)...

Runtime Analysis of Algorithms:

In general cases, we mainly used to measure and compare the worst-case theoretical running time complexities of algorithms for the performance analysis. The fastest possible running time for any algorithm is O(1), commonly referred to as Constant Running Time. In this case, the algorithm always takes the same amount of time to execute, regardless of the input size. This is the ideal runtime for an algorithm, but it’s rarely achievable. In actual cases, the performance (Runtime) of an algorithm depends on n, that is the size of the input or the number of operations is required for each input item. The algorithms can be classified as follows from the best-to-worst performance (Running Time Complexity):...

Little o and Little omega notations:

Little-o: Big-O is used as a tight upper bound on the growth of an algorithm’s effort (this effort is described by the function f(n)), even though, as written, it can also be a loose upper bound. “Little-o” (o()) notation is used to describe an upper bound that cannot be tight....

What does ‘Space Complexity’ mean?

The term Space Complexity is misused for Auxiliary Space at many places. Following are the correct definitions of Auxiliary Space and Space Complexity....

Previous Year GATE Questions:

1. What is the worst-case time complexity of inserting n elements into an empty linked list, if the linked list needs to be maintained in sorted order? More than one answer may be correct. [GATE CSE 2020] (A) Θ(n) (B) Θ(n log n) (C) Θ(n2) (D) Θ(1) Solution: Correct answer is (C)...

Contact Us