AlgoSphere

Snippets for
every algorithm

Simplified code examples and implementations of core data structures and algorithms — always accessible when you need them.

Get Started

Big-O Complexity Chart

O(log n), O(1)O(n)O(n log n)O(n^2)O(2^n)O(n!)OperationsElements

Data Structure Operations

Data StructureTime ComplexitySpace Complexity
AverageWorstWorst
AccessSearchInsertionDeletionAccessSearchInsertionDeletion
ArrayΘ(1)Θ(n)Θ(n)Θ(n)O(1)O(n)O(n)O(n)O(n)
StackΘ(n)Θ(n)Θ(1)Θ(1)O(n)O(n)O(1)O(1)O(n)
QueueΘ(n)Θ(n)Θ(1)Θ(1)O(n)O(n)O(1)O(1)O(n)
Singly-Linked ListΘ(n)Θ(n)Θ(1)Θ(1)O(n)O(n)O(1)O(1)O(n)
Doubly-Linked ListΘ(n)Θ(n)Θ(1)Θ(1)O(n)O(n)O(1)O(1)O(n)
Skip ListΘ(log(n))Θ(log(n))Θ(log(n))Θ(log(n))O(n)O(n)O(n)O(n)O(n log(n))
Hash TableN/AΘ(1)Θ(1)Θ(1)N/AO(n)O(n)O(n)O(n)
Binary Search TreeΘ(log(n))Θ(log(n))Θ(log(n))Θ(log(n))O(n)O(n)O(n)O(n)O(n)
Cartesian TreeN/AΘ(log(n))Θ(log(n))Θ(log(n))N/AO(n)O(n)O(n)O(n)
B-TreeΘ(log(n))Θ(log(n))Θ(log(n))Θ(log(n))O(log(n))O(log(n))O(log(n))O(log(n))O(n)
Red-Black TreeΘ(log(n))Θ(log(n))Θ(log(n))Θ(log(n))O(log(n))O(log(n))O(log(n))O(log(n))O(n)
Splay TreeN/AΘ(log(n))Θ(log(n))Θ(log(n))N/AO(log(n))O(log(n))O(log(n))O(n)
AVL TreeΘ(log(n))Θ(log(n))Θ(log(n))Θ(log(n))O(log(n))O(log(n))O(log(n))O(log(n))O(n)
KD TreeΘ(log(n))Θ(log(n))Θ(log(n))Θ(log(n))O(n)O(n)O(n)O(n)O(n)

Sorting Algorithms

AlgorithmTime ComplexitySpace Complexity
BestAverageWorstWorst
QuicksortΩ(n log(n))Θ(n log(n))O(n^2)O(log(n))
MergesortΩ(n log(n))Θ(n log(n))O(n log(n))O(n)
TimsortΩ(n)Θ(n log(n))O(n log(n))O(n)
HeapsortΩ(n log(n))Θ(n log(n))O(n log(n))O(1)
Bubble SortΩ(n)Θ(n^2)O(n^2)O(1)
Insertion SortΩ(n)Θ(n^2)O(n^2)O(1)
Selection SortΩ(n^2)Θ(n^2)O(n^2)O(1)
Tree SortΩ(n log(n))Θ(n log(n))O(n^2)O(n)
Shell SortΩ(n log(n))Θ(n(log(n))^2)O(n(log(n))^2)O(1)
Bucket SortΩ(n+k)Θ(n+k)O(n^2)O(n)
Radix SortΩ(nk)Θ(nk)O(nk)O(n+k)
Counting SortΩ(n+k)Θ(n+k)O(n+k)O(k)
CubesortΩ(n)Θ(n log(n))O(n log(n))O(n)

Common Algorithms

Binary Search

Efficiently find an element in a sorted array by repeatedly dividing the search range in half.

Time: O(log n)Space: O(1)

Merge Sort

Divide the array into halves, sort each half recursively, and then merge them.

Time: O(n log n)Space: O(n)

Breadth First Search (BFS)

Traverse a graph or tree level by level using a queue (First-In First-Out).

Time: O(V + E)Space: O(V)

Depth First Search (DFS)

Traverse a graph or tree as deep as possible before backtracking using recursion or a stack.

Time: O(V + E)Space: O(V)

Dijkstra’s Algorithm

Finds the shortest path from a source node to all other nodes in a weighted graph (no negative edges).

Time: O((V + E) log V)Space: O(V + E)

Union-Find (Disjoint Set Union)

Tracks and merges disjoint sets efficiently, used in cycle detection and Kruskal’s MST.

Time: O(α(n)) ≈ O(1)Space: O(n)

Kadane’s Algorithm

Finds the maximum sum of a contiguous subarray using dynamic programming.

Time: O(n)Space: O(1)

Sliding Window Technique

Use a moving window over an array or string to find optimal subranges like longest substring or max sum.

Time: O(n)Space: O(1)

Two Pointer Technique

Use two pointers to process an array or string from both ends for problems like pairs or substrings.

Time: O(n)Space: O(1)

Quick Sort

Divide the array around a pivot, sorting elements smaller to the left and larger to the right recursively.

Time: O(n log n)Space: O(log n)

Topological Sort

Orders the vertices of a directed acyclic graph such that for every directed edge u → v, u appears before v.

Time: O(V + E)Space: O(V)

Floyd’s Cycle Detection

Detects a cycle in a linked list using two pointers moving at different speeds.

Time: O(n)Space: O(1)

Kruskal’s Algorithm

Finds a Minimum Spanning Tree by sorting edges and connecting vertices using Union-Find.

Time: O(E log E)Space: O(V + E)

Prim’s Algorithm

Builds a Minimum Spanning Tree by expanding the smallest weighted edges from a starting vertex.

Time: O((V + E) log V)Space: O(V + E)