Complexity of Various Algorithms

This section shows you the complexity of various algorithms.

Table of Complexity

Below is a table summarizing the time and space complexities of common algorithms across different categories. This provides a quick reference to understand their efficiency and suitability for specific problems.

AlgorithmTimeComplexity(Best)TimeComplexity(Avg.)TimeComplexity(Worst)SpaceComplexity
Sorting
BubbleO(n)O(n²)O(n²)O(1)
SelectionO(n²)O(n²)O(n²)O(1)
InsertionO(n)O(n²)O(n²)O(1)
MergeO(n log n)O(n log n)O(n log n)O(n)
QuickO(n log n)O(n log n)O(n²)O(log n)
HeapO(n log n)O(n log n)O(n log n)O(1)
CountingO(n + k)O(n + k)O(n + k)O(k)
Searching
LinearO(1)O(n)O(n)O(1)
BinaryO(1)O(log n)O(log n)O(1)
Graph
BFSO(V + E)O(V + E)O(V + E)O(V)
DFSO(V + E)O(V + E)O(V + E)O(V)
Dijkstra’sO((V + E)log V)O((V + E)log V)O((V + E)log V)O(V + E)
Floyd-WarshallO(n³)O(n³)O(n³)O(n²)

Key:

  • n = size of input
  • k = range of input values (e.g., for counting sort)
  • V = number of vertices in a graph
  • E = number of edges in a graph

This table is a quick overview to help you compare and understand the performance of algorithms at a glance.