Hitesh Sahu
Hitesh SahuHitesh Sahu
  1. Home
  2. β€Ί
  3. posts
  4. β€Ί
  5. …

  6. β€Ί
  7. 4 Algorithmic Complexity

Loading ⏳
Fetching content, this won’t take long…


πŸ’‘ Did you know?

🍯 Honey never spoils β€” archaeologists found 3,000-year-old jars still edible.

πŸͺ This website uses cookies

No personal data is stored on our servers however third party tools Google Analytics cookies to measure traffic and improve your website experience. Learn more

Loading ⏳
Fetching content, this won’t take long…


πŸ’‘ Did you know?

🀯 Your stomach gets a new lining every 3–4 days.
Programming

    AI-AgenticAI

    AI-DeepLearning

    AI-GenAI

    AI-Infrastructure

    AI-Machine-Learning

    AI-Math

    AWS

    Azure

    kubernetes

    Management

    Programming
    • 🧱 Data Structures: Arrays, Stacks, Queues, Heaps, Hash Tables, Tries & Graphs

    • 🌲 Trees Deep Dive: BST, AVL Rotations, Red-Black Trees, B-Trees & B+ Trees

    • πŸ•ΈοΈ Graph Data Structures: Adjacency List vs Matrix, BFS & DFS

    • πŸ”’ Algorithmic Complexity: Big O From First Principles

    • πŸ”Ž Searching Algorithm Complexity πŸ“–

    • ⚑ Sorting Algorithm Complexity πŸ“–

    • πŸ—„οΈ Database Comparison πŸ“–

    • Ansible: Agentless Configuration Management

    • CI/CD Pipelines: From Commit to Production

    • Unix Internals: Processes, File Descriptors, and Syscalls

    • Programming Index


    Terraform

    Z_Appendix

Cover Image for πŸ”’ Algorithmic Complexity: Big O From First Principles
Programming

πŸ”’ Algorithmic Complexity: Big O From First Principles

Asymptotic notation from the ground up β€” deriving Big O from a growth formula, a real timing table showing what each complexity class actually costs at n = 1,000,000, and every major growth order (constant, logarithmic, linearithmic, polynomial, exponential, factorial) with real code examples.

Algorithms
Big O
Complexity
Asymptotic Notation
Study Notes
← Previous

πŸ•ΈοΈ Graph Data Structures: Adjacency List vs Matrix, BFS & DFS

Next β†’

πŸ”Ž Searching Algorithm Complexity πŸ“–

πŸ”’ Algorithmic Complexity

Asymptotic Notations

Measures the order of growth of an algorithm with respect to the number of inputs nnn.

  • Also called as Landau Notation (Edmund Landau)
Notation Bound Common Usage
Big O Upper Bound Worst-case complexity
Little o Upper Strict Bound Mathematical analysis to show one function grows strictly slower than another
Big Ξ©\OmegaΞ© Lower Bound Best-case or guaranteed lower bound
Little Ο‰ Lower Strict Bound Mathematical analysis to show one function grows strictly faster than another
Big Θ\ThetaΘ Average Bound Exact asymptotic complexity when both upper and lower bounds match

Asymptotic Notations

Example:

T(n)=5n2+3n+20T(n)=5n^2+3n+20T(n)=5n2+3n+20
Notation Result Meaning
Big OOO O(n2)O(n^2)O(n2) The function grows no faster than n2n^2n2
Little ooo o(n3)o(n^3)o(n3) The function grows strictly slower than n3n^3n3
Big Ξ©\OmegaΞ© Ξ©(n2)\Omega(n^2)Ξ©(n2) The function grows at least as fast as n2n^2n2
Little ω\omegaω ω(n)\omega(n)ω(n) The function grows strictly faster than nnn
Big Θ\ThetaΘ Θ(n2)\Theta(n^2)Θ(n2) The function grows exactly like n2n^2n2 asymptotically.

Big OOO

Represents the worst-case scenario, where the loop runs till the last element.

  • Fastest growing expression determine over all complexity.
  • Remove Constant & Remove non-dominant terms

Example.

f(n)=An+Bβ€…β€ŠβŸΉβ€…β€ŠO(n)f(n) = An + B \implies O(n)f(n)=An+B⟹O(n) f(n)=n3+n2+7nβ€…β€ŠβŸΉβ€…β€ŠO(n3)f(n) = n^3 + n^2 + 7n \implies O(n^3)f(n)=n3+n2+7n⟹O(n3)
Complexity Operations (n = 1,000,000) Approx. Time*
O(1)O(1)O(1) 1 1 ns
O(log⁑n)O(\log n)O(logn) β‰ˆ 20 20 ns
O(log⁑2n)O(\log^2 n)O(log2n) β‰ˆ 400 400 ns (0.4 ΞΌs)
O(n)O(\sqrt n)O(n​) 1,000 1 ΞΌs
O(n)O(n)O(n) 1,000,000 1 ms
O(nlog⁑n)O(n \log n)O(nlogn) β‰ˆ 20,000,000 20 ms
O(n2)O(n^2)O(n2) 1,000,000,000,000 β‰ˆ 16.7 minutes
O(n3)O(n^3)O(n3) 101810^{18}1018 β‰ˆ 31.7 years

Time Complexity


Commonly used big O notation

1. Constant time : O(1)O(1)O(1) -

Amount of work does not grow as the input size grows Fastest,

Independent of nnn

  • Single R/W
  • Stack push(), pop(), peek()
  • Hash table lookup (average case)
  • Executes the same number of operations regardless of input size

2. Logarithmic : O(log⁑n)O(\log n)O(logn) -

An algorithm has logarithmic time complexity when it reduces the problem size by a constant factor in each step, rather than processing every element.

Divide the input by a constant factor (usually 2) each iteration

Common in searching and tree-based algorithms

  • Binary Search
  • Divide & conquer

Example:

let n = 1024;

while (n > 1) {
  console.log(n);
  n = Math.floor(n / 2);
}


3. Poly Logarithmic : O((log⁑n)k)O((\log n)^k)O((logn)k)

If an algorithm performs a logarithmic amount of work multiple times, or has nested logarithmic operations, it often has polylogarithmic complexity

It grows slower than linear O(n) but faster than logarithmic O(log⁑n)O(\log n)O(logn).

Example:

let n = 1024;

for (let i = n; i > 1; i = Math.floor(i / 2)) {
for (let j = n; j > 1; j = Math.floor(j / 2)) {
  console.log(i, j);
}
}

1. Polynomial : O(nc)O(n^c)O(nc)

ccc nested loops iterating over nnn elements

Polynomial-time algorithms are generally considered efficient (or tractable) in computer science, especially compared to exponential and factorial algorithms.

Name Complexity
Linear O(n)O(n)O(n)
Quadratic O(n2)O(n^2)O(n2)
Cubic O(n3)O(n^3)O(n3)
Quartic O(n4)O(n^4)O(n4)
Quintic O(n5)O(n^5)O(n5)
General Polynomial O(nk)O(n^k)O(nk)

1.1 Linear : O(n)O(n)O(n) -

Grows proportionally to input size

  • A single Loop iterating over n elements
  • As number of elements grow time grows

1.2 Linearithmic : O(nΒ log(n))O(n \ log(n))O(nΒ log(n))

Performs logarithmic work for each of the n elements, or repeatedly divides the problem and then performs linear work during each level of recursion.

Note: not strictly polynomial, but slots in here by growth rate

Faster than quadratic O(n2)O(n^2)O(n2) but slower than O(log⁑(n))O(\log(n))O(log(n))

Combines linear and logarithmic growth

O(n)Γ—O(log⁑n)=O(nlog⁑n) O(n) \times O(\log n) = O(n \log n)O(n)Γ—O(logn)=O(nlogn)

Typical of efficient sorting algorithms

  • Merge Sort
  • Heap Sort
  • Average-case Quick Sort
  • Building a balanced Binary Search Tree
  • Fast Fourier Transform (FFT)

Example:


let n = 1024;

for (let i = 0; i < n; i++) {                      // O(n)
  for (let j = n; j > 1; j = Math.floor(j / 2)) {  // O(log n)
    console.log(i, j);
  }
}

1.3. Quadratic : O(n2)O(n^2)O(n2)

Performs nnn operations for each of the nnn elements.

O(n)Γ—O(n)=O(n2)O(n) \times O(n) = O(n^2)O(n)Γ—O(n)=O(n2)

Typical of sorting algorithms

  • Bubble Sort: O(n2)
  • Selection Sort: O(n2)

Two nested linear loops:

Example:

let n = 1024;

for (let i = 0; i < n; i++) {      // O(n)
  for (let j = 0; j < n; j++) {    // O(n)
    console.log(i, j);
  }
}

1.4. Cubic : O(n3)O(n^3)O(n3)

Performs nnn operations for each of the n2n^2n2 operations, typically due to three nested loops.

Three nested linear loops

  • Naive matrix multiplication
  • Floyd-Warshall shortest path algorithm
  • Comparing every triplet of elements
  • Brute-force solutions involving three independent variables

Example:

  let n = 100;
  
  for (let i = 0; i < n; i++) {          // O(n)
    for (let j = 0; j < n; j++) {        // O(n)
      for (let k = 0; k < n; k++) {      // O(n)
        console.log(i, j, k);
      }
    }
  }

2. Exponential : O(cn)O(c^n)O(cn)

For each element run ccc nested loops

  • Running time doubles, triples, or generally multiplies by a constant with each additional input
  • Typically caused by recursive branching
  • Becomes impractical even for moderate input sizes

If each recursive call branches into a constant number (c>1)(c>1)(c>1) of new recursive calls, the algorithm usually has exponential time complexity, O(cn)O(c^n)O(cn).

Example:

function solve(n) {
  if (n === 0) return;

  solve(n - 1);
  solve(n - 1);
}

solve(5);

O(cn)O(c^n)O(cn) grows faster than O(nc)O(n^c)O(nc).

If O(nc)O(n^c)O(nc) grows faster than O(cn)O(c^n)O(cn) then it is called superpolynomial, and in that case O(cn)O(c^n)O(cn) is called subexponential.


3. Factorial : O(n!)O(n!)O(n!)

explores every possible ordering (permutation) of the input elements.

  • Generating all permutations
  • Brute-force Traveling Salesman Problem (TSP)
  • Brute-force scheduling and assignment problems
  • Exhaustive search over all possible orderings

Factorial growth is even faster than exponential growth. It becomes impractical for surprisingly small values of n.

nΓ—(nβˆ’1)Γ—(nβˆ’2)Γ—β‹―Γ—2Γ—1=n!n \times (n-1) \times (n-2) \times \cdots \times 2 \times 1 = n!nΓ—(nβˆ’1)Γ—(nβˆ’2)Γ—β‹―Γ—2Γ—1=n!

Example;

function permute(arr, start = 0) {
  if (start === arr.length) {
    console.log(arr.join(" "));
    return;
  }

  for (let i = start; i < arr.length; i++) {
    [arr[start], arr[i]] = [arr[i], arr[start]];

    permute(arr, start + 1);

    [arr[start], arr[i]] = [arr[i], arr[start]];
  }
}

permute([1, 2, 3]);

If an algorithm has factorial complexity,

  • first ask whether it's recomputing the same work or exploring impossible solutions.
  • Memoization, dynamic programming, and pruning are the most common ways to reduce O(n!) to a much more practical complexity.

Related Posts

  • 🧱 Data Structures β€” the structures whose Access/Insertion/Deletion/Search columns use this exact Big O notation
  • πŸ”Ž Searching Algorithms β€” Binary Search's O(log⁑n)O(\log n)O(logn) is the logarithmic case worked through here
  • ⚑ Sorting Algorithms β€” Merge Sort/Quick Sort's O(nlog⁑n)O(n \log n)O(nlogn) is the linearithmic case worked through here
Hitesh Sahu
Written by Hitesh Sahu, a passionate developer and blogger.

Fri Feb 20 2026

Share This on

← Previous

πŸ•ΈοΈ Graph Data Structures: Adjacency List vs Matrix, BFS & DFS

Next β†’

πŸ”Ž Searching Algorithm Complexity πŸ“–

Programming/4-Algorithmic-Complexity
Let's work together
+49 176-2019-2523
hiteshkrsahu@gmail.com
WhatsApp
Skype
Munich πŸ₯¨, Germany πŸ‡©πŸ‡ͺ, EU
Playstore
Hitesh Sahu's apps on Google Play Store
Need Help?
Let's Connect
Navigation
Β  Home/About
Β  Skills
Β  Work/Projects
Β  Lab/Experiments
Β  Contribution
Β  Awards
Β  Art/Sketches
Β  Thoughts
Β  Contact
Links
Β  Sitemap
Β  Legal Notice
Β  Privacy Policy

Made with

NextJS logo

NextJS by

hitesh Sahu

| Β© 2026 All rights reserved.