Quantcast
Channel: Hacker News
Viewing all articles
Browse latest Browse all 737

Big-O Algorithm Complexity Cheat Sheet

$
0
0

Hi there!  This webpage covers the space and time Big-O complexities of common algorithms used in Computer Science.  When preparing for technical interviews in the past, I found myself spending hours crawling the internet putting together the best, average, and worst case complexities for search and sorting algorithms so that I wouldn't be stumped when asked about them.  Over the last few years, I've interviewed at several Silicon Valley startups, and also some bigger companies, like Yahoo, eBay, LinkedIn, and Google, and each time that I prepared for an interview, I thought to msyelf "Why oh why hasn't someone created a nice Big-O cheat sheet?".  So, to save all of you fine folks a ton of time, I went ahead and created one.  Enjoy!

Searching

AlgorithmData StructureTime ComplexitySpace Complexity
BestAverageWorstWorst
Depth First Search (DFS)TreeO(1)-O(b^d)O(|V|)
Breadth First Search (BFS)TreeO(1)-O(b^d)O(b^d)
BinaryTreeO(1)O(log(n))O(log(n))O(1)
Linear (Brute Force)ArrayO(1)O(n)O(n)O(1)

Sorting

AlgorithmData StructureTime ComplexitySpace Complexity
BestAverageWorstWorst
QuicksortArrayO(n log(n))O(n log(n))O(n^2)O(log(n))
MergesortArrayO(n log(n))O(n log(n))O(n log(n))O(n)
Bubble SortArrayO(n)O(n^2)O(n^2)O(1)
Insertion SortArrayO(n)O(n^2)O(n^2)O(1)

Data Structures

Data StructureTime ComplexitySpace Complexity
AverageWorstWorst
IndexingSearchInsertionDeletionIndexingSearchInsertionDeletion
Basic ArrayO(1)O(n)--O(1)O(n)--O(n)
Dynamic ArrayO(1)O(n)O(n)-O(1)O(n)O(n)-O(n)
Singly-Linked ListO(n)O(n)O(1)O(1)O(n)O(n)O(1)O(1)O(n)
Doubly-Linked ListO(n)O(n)O(1)O(1)O(n)O(n)O(1)O(1)O(n)
Hash Table-O(1)O(1)O(1)-O(n)O(n)O(n)O(n)
Binary Search Tree-O(log(n))O(log(n))O(log(n))-O(n)O(n)O(n)O(n)
B-Tree-O(log(n))O(log(n))O(log(n))-O(log(n))O(log(n))O(log(n))O(n)
Red-Black Tree-O(log(n))O(log(n))O(log(n))-O(log(n))O(log(n))O(log(n))O(n)
AVL Tree-O(log(n))O(log(n))O(log(n))-O(log(n))O(log(n))O(log(n))O(n)

Graphs

Node / Edge ManagementStorageAdd VertexAdd EdgeRemove VertexRemove EdgeQuery
Adjacency listO(|V|+|E|)O(1)O(1)O(|E|)O(|E|)O(|V|)
Incidence listO(|V|+|E|)O(1)O(1)O(|E|)O(|E|)O(|E|)
Adjacency matrixO(|V|^2)O(|V|^2)O(1)O(|V|^2)O(1)O(1)
Incidence matrixO(|V| ⋅ |E|)O(|V| ⋅ |E|)O(|V| ⋅ |E|)O(|V| ⋅ |E|)O(|V| ⋅ |E|)O(|E|)

|V| denotes the number of vertices; |E| denotes the number of edges.

Big-O Complexity Graph

Big O Complexity Graph

Contribute

Edit these tables!

Authors:

comments powered by

Viewing all articles
Browse latest Browse all 737

Trending Articles