Skip to main content

Command Palette

Search for a command to run...

Algorithm Time Complexity Overview

Published
•View as Markdown
K

I hate bird

Note:

The following complexities represent typical cases for commonly used algorithms and data structures.
Actual performance may vary depending on input size, data distribution, and problem constraints.

1. Sorting Algorithms

AlgorithmAverage Time ComplexityWorst Time ComplexitySpace Complexity
Bubble SortO(N²)O(N²)O(1)
Selection SortO(N²)O(N²)O(1)
Insertion SortO(N²)O(N²)O(1)
Merge SortO(N log N)O(N log N)O(N)
Quick SortO(N log N)O(N²)O(log N)
Heap SortO(N log N)O(N log N)O(1)
Counting SortO(N + K)O(N + K)O(K)
Radix SortO(NK)O(NK)O(N + K)

2. Searching Algorithms

AlgorithmTime Complexity
Linear SearchO(N)
Binary SearchO(log N)
Hash Search (Map)O(1) average / O(N) worst

3. Data Structure Operations

Arrays / Vectors

  • Access: O(1)
  • Search: O(N)
  • Insert/Delete (middle): O(N)
  • Insert/Delete (end): O(1) amortized

Stack / Queue

  • Push / Pop: O(1)
  • Search: O(N)

Heap (Priority Queue)

  • Insert: O(log N)
  • Extract Min/Max: O(log N)
  • Build Heap: O(N)

Hash Map (unordered_map)

  • Insert/Search/Delete: O(1) average, O(N) worst

4. Graph Algorithms

AlgorithmTime Complexity (V = vertices, E = edges)
BFS / DFSO(V + E)
Dijkstra (heap)O((V + E) log V)
Floyd-WarshallO(V³)
Kruskal (MST)O(E log E)
Prim (MST, heap)O(E log V)

5. Dynamic Programming

  • Fibonacci (Top-down/Bottom-up): O(N)
  • LCS (Longest Common Subsequence): O(N × M)
  • 0/1 Knapsack: O(N × W) (N = items, W = weight capacity)
  • LIS (Longest Increasing Subsequence):
    • Basic DP: O(N²)
    • Optimized with Binary Search: O(N log N)

6. String Algorithms

AlgorithmTime Complexity
KMP Pattern SearchO(N + M)
Rabin-KarpO(N + M) average / O(NM) worst
Z-AlgorithmO(N + M)
Trie Insert/SearchO(L) (L = length of string)

Algorithm

Part 1 of 2

Series for coding interview preparation. The posts will mainly focus on problems I couldn’t solve. I will prioritize writing down concepts and explanations that I might need to revisit later.

Up next

Prefix Sum

A problem that processes a specific range in an N × M array may produce the correct answer using a simple for loop, but it can fail the efficiency test due to time limit issues. Drawbacks of Using a Naive For Loop Time complexity can increase up to ...

More from this blog