C Programming • Introduction to Computer, Programming & algorithms
C Programming / Complexity notations

Complexity notations

Notes 1 Introduction to Computer, Programming & algorithms

Complexity analysis is used to measure how much time and memory an algorithm requires as the size of its input increases. It helps us compare algorithms and understand their efficiency.

Notes

What is Complexity Analysis?

Complexity analysis is used to measure how much time and memory an algorithm requires as the size of its input increases. It helps us compare algorithms and understand their efficiency.

šŸ’” In Simple Words

Complexity tells us how an algorithm's work and memory requirement grow when the amount of data becomes larger.

Why Do We Need Complexity?

  • To compare different algorithms.
  • To understand how an algorithm behaves for large input.
  • To choose a more efficient solution.
  • To study time and memory requirements.

Types of Complexity

Type Measures
Time Complexity How the number of operations grows with input size.
Space Complexity How the memory requirement grows with input size.

Time Complexity

Time complexity describes how the number of operations performed by an algorithm grows as the input size n increases.

šŸ“Œ Important

Time complexity usually talks about the number of steps or operations, not the actual time in seconds.

Example

FOR i = 1 TO n ↓ Perform one operation ↓ Total operations ā‰ˆ n ↓ Time Complexity = O(n)

Space Complexity

Space complexity describes how much memory an algorithm requires as the input size increases. It includes the memory needed for the input and the extra memory used by the algorithm.

Example

If an algorithm uses only a fixed number of extra variables, its extra space remains constant even when n becomes larger.

Fixed Extra Memory ↓ Does not grow with n ↓ Space Complexity = O(1)

Asymptotic Analysis

Asymptotic analysis studies the growth of an algorithm for very large input sizes. It focuses on the growth rate and normally ignores constants and lower-order terms.

šŸ’” Example

3n and 5n both have linear growth, so both are represented as O(n).

Common Growth Rates

Notation Name Example
O(1) Constant Accessing an array element
O(log n) Logarithmic Binary search
O(n) Linear Linear search
O(n log n) Linearithmic Merge sort
O(n²) Quadratic Bubble sort
O(2ⁿ) Exponential Some subset-generation problems

Big-O Notation — O(g(n))

Big-O notation represents an asymptotic upper bound on an algorithm's growth. It is commonly used to describe worst-case time complexity.

šŸ’” In Simple Words

Big-O tells us how much an algorithm can grow at most, especially in the worst case.

Example

for i = 0 to n ↓ Runs about n times ↓ O(n)

Big-Ī© (Omega) Notation — Ī©(g(n))

Omega notation represents an asymptotic lower bound. It describes the minimum amount of growth or work required by an algorithm.

šŸ’” In Simple Words

Omega tells us the minimum growth of an algorithm. It is commonly associated with the best-case situation.

Example

In a linear search, if the required element is found at the first position, only one comparison may be required.

Best Case ↓ Minimum Work ↓ Ī©(1)

Big-Θ (Theta) Notation — Θ(g(n))

Theta notation represents a tight asymptotic bound. It is used when the upper and lower bounds have the same growth rate.

šŸ’” In Simple Words

Theta tells us the exact growth order of an algorithm within constant factors when the upper and lower bounds match.

Example

n Operations in Best Case ↓ n Operations in Worst Case ↓ Θ(n)

Big-O, Big-Ω and Big-Θ

Notation Meaning Simple Idea
O(g(n)) Upper bound Maximum / worst-case growth
Ī©(g(n)) Lower bound Minimum / best-case growth
Θ(g(n)) Tight bound Matching upper and lower growth

Comparison of Growth Rates

O(1) ↓ O(log n) ↓ O(n) ↓ O(n log n) ↓ O(n²) ↓ O(2ⁿ)

In general, a slower-growing complexity is preferred when working with large input sizes.

🧠 Quick Memory Trick

O → Upper Bound    |    Ī© → Lower Bound    |    Θ → Tight Bound

Simple Example: Finding a Number

Suppose we search for a number in an unsorted array containing n elements.

Case Situation Complexity
Best Case Element is found at the first position. Ī©(1)
Worst Case Element is at the last position or absent. O(n)

Quick Revision

Concept Remember
Time Complexity Growth in number of operations.
Space Complexity Growth in memory requirement.
Big-O Upper bound / commonly used for worst case.
Big-Ī© Lower bound / commonly associated with best case.
Big-Θ Tight bound.

Important Exam Questions

Short Answer Questions

  1. What is complexity analysis?
  2. What is time complexity?
  3. What is space complexity?
  4. What is asymptotic analysis?
  5. What is Big-O notation?
  6. What is Big-Ī© notation?
  7. What is Big-Θ notation?
  8. Write the common growth rates of algorithms.

Long Answer Questions

  1. Explain time complexity and space complexity with suitable examples.
  2. Explain Big-O, Big-Ω and Big-Θ notations.
  3. Explain asymptotic analysis and its importance in algorithm analysis.
  4. Explain different common growth rates with examples.
šŸŽ„ Recommended Learning

Watch a beginner-friendly explanation of time complexity, space complexity and Big-O notation.

ā–¶ Watch: Time Complexity & Big-O — Hindi

šŸ“ Handwritten Notes

A short handwritten-style revision sheet for complexity notations will be provided here.

🧠 Mind Map

Use the mind map for quick revision of time complexity, space complexity and asymptotic notations.