Algorithm Complexity

0
(0)

Algorithm complexity measures how efficiently an algorithm uses time and memory as the input size increases.

It is one of the most important concepts in Data Structures and Algorithms (DSA).

1. Why Complexity Matters

Suppose we want to search for a number in an array:

A = [10, 25, 30, 45, 60, 75, 90]

For a small array, almost any algorithm seems fast.

But imagine:

n = 1,000,000,000

An inefficient algorithm could take an extremely long time.

Complexity lets us predict how an algorithm behaves as n grows.

2. Two Main Types of Complexity

A. Time Complexity

Measures how much time/number of operations an algorithm requires.

Example:

for (int i = 0; i < n; i++) {
    cout << i;
}

The loop executes n times.

Therefore:

 

T(n)=O(n)T(n) = O(n) 

This is called linear time.

B. Space Complexity

Measures how much additional memory an algorithm requires.

Example:

int sum = 0;

for (int i = 0; i < n; i++) {
    sum += i;
}

Only a few variables are used regardless of n.

Therefore:

 

S(n)=O(1)S(n) = O(1) 

This is constant space.

3. Big-O Notation

The most commonly used notation is Big-O.

Big-O describes the growth rate of an algorithm.

Common complexities, from generally best to worst:

ComplexityNameExample
O(1)ConstantArray access
O(log n)LogarithmicBinary search
O(n)LinearLinear search
O(n log n)LinearithmicMerge sort
O(n²)QuadraticBubble sort
O(n³)CubicSome matrix algorithms
O(2ⁿ)ExponentialSome recursive problems
O(n!)FactorialBrute-force permutations

The important idea is:

 

As n increases, how quickly does the work grow?\boxed{\text{As }n\text{ increases, how quickly does the work grow?}} 

4. O(1) — Constant Complexity

The algorithm performs approximately the same amount of work regardless of input size.

int first = arr[0];

Whether the array contains:

10 elements

or

10,000,000 elements

accessing arr[0] takes constant-time work.

Therefore:

 

O(1)\boxed{O(1)} 

Example

int getFirst(int arr[]) {
    return arr[0];
}

5. O(n) — Linear Complexity

Work increases directly with input size.

for (int i = 0; i < n; i++) {
    cout << arr[i];
}

If:

n = 10      → 10 iterations
n = 100     → 100 iterations
n = 1,000   → 1,000 iterations

Therefore:

 

O(n)\boxed{O(n)} 

Example: Linear Search

int search(int arr[], int n, int x) {
    for (int i = 0; i < n; i++) {
        if (arr[i] == x)
            return i;
    }

    return -1;
}

Worst case:

 

O(n)O(n) 

because we may need to inspect every element.

6. O(log n) — Logarithmic Complexity

The input is repeatedly reduced by a factor, commonly by half.

The classic example is Binary Search.

Suppose:

n = 16

Binary search reduces the search space:

16 → 8 → 4 → 2 → 1

Only about:

 

log2(16)=4\log_2(16)=4 

steps are needed.

Therefore:

 

O(logn)\boxed{O(\log n)} 

Binary Search

int binarySearch(int arr[], int n, int x) {
    int left = 0;
    int right = n - 1;

    while (left <= right) {
        int mid = left + (right - left) / 2;

        if (arr[mid] == x)
            return mid;

        if (arr[mid] < x)
            left = mid + 1;
        else
            right = mid - 1;
    }

    return -1;
}

Requirement: the array must be sorted.

7. O(n²) — Quadratic Complexity

Usually occurs when one loop is nested inside another.

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        cout << i << " " << j;
    }
}

The outer loop executes n times.

For each outer iteration, the inner loop executes n times.

Therefore:

 

n×n=n2n \times n = n^2 

So:

 

O(n2)\boxed{O(n^2)} 

Example

For:

n = 10

approximately:

100 operations

For:

n = 1,000

approximately:

1,000,000 operations

This illustrates why quadratic algorithms become expensive quickly.

8. O(n³) — Cubic Complexity

Three nested loops commonly produce cubic complexity:

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
            // operation
        }
    }
}

Therefore:

 

n×n×n=n3n \times n \times n = n^3 

O(n3)\boxed{O(n^3)} 

9. O(n log n)

This is an important complexity in efficient sorting algorithms.

Examples include:

  • Merge Sort
  • Heap Sort
  • Average-case Quick Sort

Conceptually, an algorithm may divide the problem into smaller pieces:

 

logn\log n 

while processing approximately:

 

nn 

elements at each level.

Thus:

 

O(nlogn)\boxed{O(n\log n)} 

10. Exponential Complexity — O(2ⁿ)

Exponential algorithms grow extremely rapidly.

A simple example:

int fib(int n) {
    if (n <= 1)
        return n;

    return fib(n - 1) + fib(n - 2);
}

The naive recursive Fibonacci algorithm has approximately:

 

O(2n)\boxed{O(2^n)} 

time complexity.

For large nthis becomes impractical.

Using dynamic programming can reduce Fibonacci calculation to:

 

O(n)O(n) 

11. Factorial Complexity — O(n!)

Factorial complexity is even worse.

For example, generating every possible permutation of n objects requires:

 

n!n! 

possibilities.

For:

n = 5

 

5!=1205! = 120 

For:

n = 10

 

10!=3,628,80010! = 3,628,800 

For:

n = 20

 

20!=2,432,902,008,176,640,00020! = 2,432,902,008,176,640,000 

This is why brute-force permutation algorithms become infeasible very quickly.

12. Comparing Growth Rates

A useful order to memorize is:

 

O(1)<O(logn)<O(n)<O(nlogn)<O(n2)<O(n3)<O(2n)<O(n!)\boxed{ O(1) < O(\log n) < O(n) < O(n\log n) < O(n^2) < O(n^3) < O(2^n) < O(n!) } 

As n becomes very large, algorithms toward the right generally become much less practical.

13. How to Calculate Time Complexity

Consider:

for (int i = 0; i < n; i++) {
    cout << i;
}

The loop runs n times:

 

T(n)=nT(n)=n 

Therefore:

 

O(n)\boxed{O(n)} 

Example 2

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        cout << i + j;
    }
}

Number of operations:

 

n×n=n2n \times n = n^2 

Therefore:

 

O(n2)\boxed{O(n^2)} 

Example 3

for (int i = 0; i < n; i++) {
    cout << i;
}

for (int j = 0; j < n; j++) {
    cout << j;
}

The loops are sequential, not nested:

 

n+n=2nn+n=2n 

Drop the constant:

 

O(n)\boxed{O(n)} 

14. Important Rules for Big-O

Rule 1 — Ignore constants

If:

 

T(n)=5nT(n)=5n 

then:

 

O(n)\boxed{O(n)} 

Not O(5n).

Rule 2 — Keep the dominant term

If:

 

T(n)=n2+n+10T(n)=n^2+n+10 

the dominant term is:

 

n2n^2 

Therefore:

 

O(n2)\boxed{O(n^2)} 

Rule 3 — Sequential operations are usually added

O(n) + O(n)

becomes:

 

O(2n)=O(n)O(2n)=O(n) 

Rule 4 — Nested operations are usually multiplied

O(n) × O(n)

becomes:

 

O(n2)\boxed{O(n^2)} 

15. Best, Average and Worst Case

Complexity can also be analyzed according to the input situation.

Consider linear search:

[10, 20, 30, 40, 50]

Searching for 10:

1 comparison

Searching for 30:

3 comparisons

Searching for 50:

5 comparisons

Searching for something that doesn’t exist:

5 comparisons

So:

CaseComplexity
Best caseO(1)
Average caseO(n)
Worst caseO(n)

16. Complexity and Data Structures

Complexity is closely connected to data structures.

For example:

OperationArrayLinked ListHash Table*
Access by indexO(1)O(n)
SearchO(n)O(n)O(1) average
Insert at beginningO(n)O(1)O(1) average
Delete at beginningO(n)O(1)O(1) average

*Hash-table complexities are typically average-case; worst-case behavior can differ.

This is why choosing the correct data structure is critical.

17. Time vs Space Trade-off

Sometimes we can make an algorithm faster by using more memory.

For example, suppose we repeatedly need to determine whether a value exists.

A naive approach might search an array every time:

 

O(n)O(n) 

per lookup.

If we build a hash table, lookup can be approximately:

 

O(1)O(1) 

on average.

But the hash table requires additional memory.

This is called a:

 

Time-Space Trade-off\boxed{\text{Time-Space Trade-off}} 

18. A Practical Example

Suppose you have:

int arr[1000000];

and need to find a value.

Linear Search

O(n)

Potentially up to one million elements examined.

Binary Search

If the array is sorted:

O(log n)

Approximately:

 

log2(1,000,000)20\log_2(1,000,000)\approx20 

comparisons.

That’s a dramatic difference.

19. Complexity Analysis Cheat Sheet

Code patternComplexity
arr[5]O(1)
One simple loopO(n)
Two separate simple loopsO(n)
Two nested loopsO(n²)
Three nested loopsO(n³)
Divide input by 2 repeatedlyO(log n)
Loop + binary divisionOften O(n log n)
Generate all subsetsO(2ⁿ)
Generate all permutationsO(n!)

20. What You Should Master

For DSA, learn algorithm complexity in this order:

Algorithm
   ↓
Input size n
   ↓
Count basic operations
   ↓
Construct T(n)
   ↓
Remove constants
   ↓
Keep dominant term
   ↓
Big-O complexity

For example:

for (int i = 0; i < n; i++) {       // n
    for (int j = 0; j < n; j++) {   // n
        // operation
    }
}

Therefore:

 

T(n)=n×n=n2T(n)=n\times n=n^2 

and finally:

 

O(n2)\boxed{O(n^2)} 

Key takeaway

Algorithm complexity is fundamentally about scalability. An algorithm that works perfectly for n = 100 may be unusable for n = 1,000,000. Learning to recognize O(1), O(log n), O(n), O(n log n), O(n²), and exponential growth is the foundation for analyzing algorithms and choosing efficient data structures.

Algorithm Complexity — 5 Practice Questions with Solutions

Question 1

What is the time complexity of the following code?

for (int i = 0; i < n; i++) {
    cout << i;
}

Solution:

The loop starts at 0 and runs until i < n.

Therefore, it executes approximately n times.

 

T(n)=nT(n)=n

 

So the time complexity is:

 

O(n)\boxed{O(n)}

 

Answer: O(n) — Linear complexity

Question 2

Determine the time complexity:

int x = arr[0];

for (int i = 0; i < n; i++) {
    cout << arr[i];
}

Solution:

There are two operations:

  1. arr[0]O(1)
  2. The loop → O(n)

Therefore:

 

O(1)+O(n)O(1)+O(n)

 

We keep the dominant term:

 

O(n)\boxed{O(n)}

 

Answer: O(n)

Question 3

Determine the time complexity of this nested loop:

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        cout << i << " " << j;
    }
}

Solution:

The outer loop runs:

 

nn

 

times.

For every iteration of the outer loop, the inner loop also runs:

 

nn

 

times.

Therefore:

 

T(n)=n×nT(n)=n\times n

 

T(n)=n2T(n)=n^2

 

Hence:

 

O(n2)\boxed{O(n^2)}

 

Answer: O(n²) — Quadratic complexity

Question 4

What is the time complexity?

for (int i = 1; i < n; i *= 2) {
    cout << i;
}

Solution:

Notice that i is multiplied by 2 every iteration:

1
2
4
8
16
32
64
...

After k iterations:

 

i=2ki=2^k

 

The loop stops when:

 

2kn2^k \geq n

 

Taking logarithms:

 

k=log2nk=\log_2 n

 

Therefore:

 

O(logn)\boxed{O(\log n)}

 

Answer: O(log n) — Logarithmic complexity

Question 5

Determine the time complexity of the following code:

for (int i = 0; i < n; i++) {

    for (int j = 1; j < n; j *= 2) {

        cout << i << " " << j;
    }
}

Solution:

Let’s analyze each loop separately.

Outer loop

for (int i = 0; i < n; i++)

Runs:

 

nn

 

times.

Therefore:

 

O(n)O(n)

 

Inner loop

for (int j = 1; j < n; j *= 2)

The values are approximately:

1 → 2 → 4 → 8 → 16 → 32 → ...

Therefore, it runs:

 

O(logn)O(\log n)

 

times.

Combine them

The inner loop executes log n times for each of the n outer iterations.

Therefore:

 

T(n)=nlognT(n)=n\log n

 

Hence:

 

O(nlogn)\boxed{O(n\log n)}

 

Answer: O(n log n) — Linearithmic complexity

Final Practice Summary

QuestionDifficultyPatternComplexity
1SimpleOne loopO(n)
2SimpleConstant + loopO(n)
3MediumNested loopsO(n²)
4Medium/HardDoublingO(log n)
5HardLoop + doubling loopO(n log n)

Quick rule to remember

One loop              → O(n)
Nested loops          → O(n²)
Three nested loops    → O(n³)
Divide by 2 each time → O(log n)
n × log n             → O(n log n)

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?


Explore More IT Terms


Share this term: Facebook X LinkedIn WhatsApp Email

Leave a Reply

Your email address will not be published. Required fields are marked *

Q&a tutorial forum on government.