big-o algorithm complexity cheat sheet
DESCRIPTION
This webpage covers the space and time Big-O complexities of common algorithms used in Computer ScienceTRANSCRIPT
-
5/5/13 Big-O Algorithm Complexity Cheat Sheet
bigocheatsheet.com 1/15
I receive
$3.00 / wkon Gittip.
Big-O Cheat Sheet
SearchingSorting
Data StructuresHeaps
GraphsChart
Comments
TweetTweet
980 285
Know Thy Complexities!
Hi there! This webpage covers the space and time Big-O complexities of common algorithms used in Computer Science. When preparing for technicalinterviews in the past, I found myself spending hours crawling the internet putting together the best, average, and worst case complexities for search and sortingalgorithms 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 somebigger 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!
GoodFairPoor
1.4k
Like
-
5/5/13 Big-O Algorithm Complexity Cheat Sheet
bigocheatsheet.com 2/15
Searching
Algorithm Data Structure Time Complexity Space Complexity
Average Worst Worst
Depth First Search (DFS) Graph of |V| vertices and |E| edges - O(|E| + |V|) O(|V|)
Breadth First Search (BFS) Graph of |V| vertices and |E| edges - O(|E| + |V|) O(|V|)
Binary search Sorted array of n elements O(log(n)) O(log(n)) O(1)
Linear (Brute Force) Array O(n) O(n) O(1)
Shortest path by Dijkstra,
using a Min-heap as priority queueGraph with |V| vertices and |E| edges O((|V| + |E|) log |V|)O((|V| + |E|) log |V|)O(|V|)
Shortest path by Dijkstra,
using an unsorted array as priority queueGraph with |V| vertices and |E| edges O(|V|^2) O(|V|^2) O(|V|)
Shortest path by Bellman-Ford Graph with |V| vertices and |E| edges O(|V||E|) O(|V||E|) O(|V|)
Sorting
Algorithm Data Structure Time Complexity Worst Case Auxiliary Space Complexity
Best Average Worst Worst
Quicksort Array O(n log(n))O(n log(n))O(n^2) O(log(n))
Mergesort Array O(n log(n))O(n log(n))O(n log(n))O(n)
Heapsort Array O(n log(n))O(n log(n))O(n log(n))O(1)
Bubble Sort Array O(n) O(n^2) O(n^2) O(1)
Insertion Sort Array O(n) O(n^2) O(n^2) O(1)
Select Sort Array O(n^2) O(n^2) O(n^2) O(1)
Bucket Sort Array O(n+k) O(n+k) O(n^2) O(nk)
-
5/5/13 Big-O Algorithm Complexity Cheat Sheet
bigocheatsheet.com 3/15
Bucket Sort Array O(n+k) O(n+k) O(n^2) O(nk)
Radix Sort Array O(nk) O(nk) O(nk) O(n+k)
Data Structures
Data Structure Time Complexity Space Complexity
Average Worst Worst
Indexing Search Insertion Deletion Indexing Search Insertion Deletion
Basic Array O(1) O(n) - - O(1) O(n) - - O(n)
Dynamic Array O(1) O(n) O(n) - O(1) O(n) O(n) - O(n)
Singly-Linked List O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)
Doubly-Linked List O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)
Skip List O(n) O(log(n))O(log(n))O(log(n))O(n) O(n) O(n) O(n) O(n log(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)
Heaps
Heaps Time Complexity
Heapify Find Max Extract Max Increase Key Insert Delete Merge
Linked List (sorted) - O(1) O(1) O(n) O(n) O(1) O(m+n)
Linked List (unsorted)- O(n) O(n) O(1) O(1) O(1) O(1)
-
5/5/13 Big-O Algorithm Complexity Cheat Sheet
bigocheatsheet.com 4/15
Linked List (unsorted)- O(n) O(n) O(1) O(1) O(1) O(1)
Binary Heap O(log(n))O(1) O(log(n)) O(log(n)) O(log(n))O(log(n)) O(m+n)
Binomial Heap - O(log(n))O(log(n)) O(log(n)) O(log(n))O(log(n)) O(log(n))
Fibonacci Heap - O(1) O(log(n))* O(1)* O(1) O(log(n))*O(1)
Graphs
Node / Edge Management Storage Add Vertex Add Edge Remove Vertex Remove Edge Query
Adjacency list O(|V|+|E|) O(1) O(1) O(|V| + |E|) O(|E|) O(|V|)
Incidence list O(|V|+|E|) O(1) O(1) O(|E|) O(|E|) O(|E|)
Adjacency matrix O(|V|^2) O(|V|^2) O(1) O(|V|^2) O(1) O(1)
Incidence matrix O(|V| |E|)O(|V| |E|)O(|V| |E|)O(|V| |E|) O(|V| |E|)O(|E|)
Notation for asymptotic growth
letter bound growth
(theta) upper and lower, tight[1] equal[2]
(big-oh) O upper, tightness unknown less than or equal[3]
(small-oh) o upper, not tight less than
(big omega) lower, tightness unknown greater than or equal
(small omega) lower, not tight greater than
[1] Big O is the upper bound, while Omega is the lower bound. Theta requires both Big O and Omega, so that's why it's referred to as a tight bound (it must beboth the upper and lower bound). For example, an algorithm taking Omega(n log n) takes at least n log n time but has no upper limit. An algorithm taking Theta(n
log n) is far preferential since it takes AT LEAST n log n (Omega n log n) and NO MORE THAN n log n (Big O n log n).SO
-
5/5/13 Big-O Algorithm Complexity Cheat Sheet
bigocheatsheet.com 5/15
[2] f(x)=(g(n)) means f (the running time of the algorithm) grows exactly like g when n (input size) gets larger. In other words, the growth rate of f(x) isasymptotically proportional to g(n).
[3] Same thing. Here the growth rate is no faster than g(n). big-oh is the most useful because represents the worst-case behavior.
In short, if algorithm is __ then its performance is __
algorithm performance
o(n) < n
O(n) n
(n) = n
(n) n
(n) > n
Big-O Complexity Chart
-
5/5/13 Big-O Algorithm Complexity Cheat Sheet
bigocheatsheet.com 6/15
Contribute
Edit these tables!
Authors:
-
5/5/13 Big-O Algorithm Complexity Cheat Sheet
bigocheatsheet.com 7/15
1. Eric Rowell
2. Quentin Pleple3. Nick Dizazzo
4. Michael Abed5. Adam Forsyth6. Jay Engineer
7. Josh Davis8. makosblade9. Alejandro Ramirez
10. Joel Friedly11. Eric Lefevre-Ardant
12. Thomas Dybdahl Ahle
67 comments
What's this?AROUND THE WEB
The Best Careers for Starting Over After 55 eHow Comedian Face Off: Which One Do You Think Is Better Looking?Comedy Central
The Hottest Gray Hair Color Trend 2013 Hair Color For Women Why Office 365 is a better deal than Office 2013 Microsoft
The Unhealthiest "Healthy" Foods Stack Spend Smartly: 5 Things You Shouldn't Do When Money is Tight Investopedia
Ten Myths and Realities of Independent Work Arrangements Kelly OCG
Worst-Kept Secret: Sex at Work Businessw eek
Leave a message...
BestBest CommunityCommunity ShareShare
27
-
5/5/13 Big-O Algorithm Complexity Cheat Sheet
bigocheatsheet.com 8/15
BestBest CommunityCommunity ShareShare
Reply
Gokce Toykuyu 6 months ago
Could we add some tree algorithms and complexities? Thanks. I really like the Red-Black trees ;)
17
Reply
ericdrowell 6 months agoM o d Gokce Toykuyu
Excellent idea. I'll add a section that compares insertion, deletion, and search complexities for specific data structures
6
Reply
Michael Mitchell a day ago
This is great. Maybe you could include some resources (links to khan academy, mooc etc) that would explain each of these
concepts for people trying to learn them.
13
Reply
Amanda Harl in a day ago Michael Mitchell
Yes! Please & thank you
6
Reply
Valent in Stanc iu a day ago
1. Deletion/insertion in a single linked list is implementation dependent. For the question of "Here's a pointer to an element, how
much does it take to delete it?", single-linked lists take O(N) since you have to search for the element that points to the element being
deleted. Double-linked lists solve this problem.
2. Hashes come in a million varieties. However with a good distribution function they are O(logN) worst case. Using a double hashing
algorithm, you end up with a worst case of O(loglogN).
3. For trees, the table should probably also contain heaps and the complexities for the operation "Get Minimum".
3
s igmaalgebra 21 hours ago
You omitted an in-place, guaranteed O(n log(n)) array sort,
e.g., heap sort. You omitted radix sort that can be faster
than any of the algorithms you mentioned.
Share
Share
Share
Share
Share
-
5/5/13 Big-O Algorithm Complexity Cheat Sheet
bigocheatsheet.com 9/15
Reply
Might mention SAT and related problems in NP-complete
where the best known algorithm for
a problem of size n has O(2 n^).
Might include an actual, precise definition of O().
2
Reply
Guest a day ago
Finals are already over... This should have been shared a week ago! Would have saved me like 45 minutes of using Wikipedia.
2
Reply
Gbor Ndai a day ago
Nice.
2
Reply
Nir Alfas i a day ago
Complexity forBFS/DFS is O(|V| + |E|), since we're traversing each node/edge only once. Good job!
2
Reply
Antoine Grondin a day ago
I think DFS and BFS, under Search, would be more appropriate listed as Graph instead of Tree.
2
Reply
ericdrowell 16 hours agoM o d Antoine Grondin
Fixed! Thanks
Reply
qpleple a day ago Antoine Grondin
Agreed
maxw3st 6 months ago
Share
Share
Share
Share
Share
Share
Share
-
5/5/13 Big-O Algorithm Complexity Cheat Sheet
bigocheatsheet.com 10/15
Reply
maxw3st 6 months ago
This gives me some excellent homework to do of a variety I'm not getting in classes. Thank you.
2
Reply
tempire a day ago
This chart seems misleading. Big O is worst case, not average case; ~ is average case. O(...) shouldn't be used in the average
case columns.
2 1
Reply
ericdrowell 19 hours agoM o d tempire
I'll try to clarify that. Thanks!
Reply
qwertykeyboard 7 hours ago
It would be very helpful to have export to PDF. Thx
1
Reply
rcbarnes 19 hours ago
You might want to list the increasingly popular (i.e. I've thought about it more often lately :-P) in-place variant of merge sort, or at
least give that item a footnote to the effect of "can be implemented with O(1) space complexity at the expense of the worst-case runtime,
which becomes O(n 2^) because of data position swaps." ( Stack overflow sanity-checked my recollection of the characteristics here:
http://stackoverflow.com/quest... )
1
Reply
Apphacker a day ago
Very nice. Here are some more to look at to add to this:
http://xlinux.nist.gov/dads/
I think also Wikipedia style animations for how these algorithms work would be cool too. Maybe if I have some time I can help out.
1
Vijayakumar Ramdoss 3 months ago
Thanks a lot
Share
Share
Share
Share
Share
Share
-
5/5/13 Big-O Algorithm Complexity Cheat Sheet
bigocheatsheet.com 11/15
Reply
Thanks a lot
1
Reply
ericdrowell 3 months agoM o d Vijayakumar Ramdoss
no prob :)
Reply
Stephane Duguay 2 hours ago
Hi, I'd like to use a french version of this page in class... should I translate it on another website or you can support localisation and I
do the data entering for french? I'm interested!
Reply
Wensheng Chen 2 hours ago
Hi, can you add a last updated timestamp? This is awesome, keep up the good work. :)
Reply
Wensheng Chen 2 hours ago Wensheng Chen
On each algorithm or data structure, it would be better if we can also provide links to actual implementation of each in
different programming languages.
E.g. Click on Dynamic Array, then you see a list of programming languages and their implementation of Dynamic Array.
Reply
Alexander 3 hours ago
QuickSort worst case space complexity is O(n).
Reply
Wensheng Chen 2 hours ago Alexander
In place partition does it in O(log n)
Reply
Vivek Mahadevan 4 hours ago
How about a wiki link to the respective algorithms along with their names?
Share
Share
Share
Share
Share
Share
Share
Share
-
5/5/13 Big-O Algorithm Complexity Cheat Sheet
bigocheatsheet.com 12/15
Reply
Reply
ct01 4 hours ago
Why is space complexity O(|V|) "good" in some cases and "fair" in others?
Reply
Xiao Ma 5 hours ago
dijkstra unsorted array to implement priority_queue the time maybe O(VV+EV) not OV*V
Reply
Xiao Ma 5 hours ago Xiao Ma
Okay,my fault,use array:V times Extract_min operation + E = O(V*V),but use Heap:Extract V*logV + E*logV. opeation
Reply
Carlos Lpez-Camey 6 hours ago
Good stuff, thanks. Here are some complexities on intersecting two lists: http://ad-teaching.informatik....
Reply
Jason Sachs 7 hours ago
you have an error in the data structure space complexities. O(n) should be green (best possible); skiplist should get a yellow.
Reply
12 hours ago
This could save my day in the future.
Thanks anyway.
Reply
b3h3m0th 12 hours ago
great work, many thanks
Kris tofer Karlsson 13 hours ago
Great stuff! But why is the space complexity for skip lists O(n log(n)) marked as fair while the others with O(n) are marked as poor?
Surely O(n) is better than O(n log(n))
Share
Share
Share
Share
Share
Share
Share
Share
-
5/5/13 Big-O Algorithm Complexity Cheat Sheet
bigocheatsheet.com 13/15
Reply
Surely O(n) is better than O(n log(n))
Reply
Timo 13 hours ago
Great piece of work, very much appreciated!
One thing: You should explain what the k in Bucket and Radix Sort is.
Reply
Thomas Womack 14 hours ago
How come the O(n) worst-case space for merge sort is red, and the O(nk) and O(n+k) worst-case space for bucket and radix sorts
yellow?
Reply
AmitK 14 hours ago
Its pretty handy!
Reply
soulmachine 16 hours ago
awesome!
Reply
Jon Renner 17 hours ago
This is god's work.
Reply
Edward Chiou 17 hours ago
Might want to add heap and heap sort to this collection as well.
Reply
thiagopnts 17 hours ago
Would be nice to add skip lists too :)
Share
Share
Share
Share
Share
Share
Share
Share
-
5/5/13 Big-O Algorithm Complexity Cheat Sheet
bigocheatsheet.com 14/15
Reply
Reply
Anonymous a day ago
You're missing heapsort!
Reply
IvanKuck ir a day ago
Do you really find this useful?
When talking about complexity, you must talk about some specific algorithm. But when you know the algorithm, you already know the
complexity, am I wrong? Does anybody just learns the paris algorithm_name : complexity, without any idea how algorithm works? OMG...
Reply
Chris Feilbach 17 hours ago IvanKuckir
I don't know every algorithm out there, including some of these, because I am on the hardware side of computer science. So
yes, even though I know some of these, I don't know all of them, and therefore, I found it useful.
Anyway, complexity is not always apparent. I've worked with algorithms that are O(n 2^.836) ... how is that obvious?
Reply
IvanKuck ir 4 hours ago Chris Feilbach
You are probably talking about some fast matrix multiplication, aren't you? :) I have implemented Strassen Algorithm
once, it has complexity around n 2^.8...
Reply
ericdrowell 19 hours agoM o d IvanKuckir
have you never had a technical interview before?
Reply
IvanKuck ir 4 hours ago ericdrowell
No, I am still a student. And I think, that if employer wants you to know just algorithm complexity, but not the whole
alogrithm, there is something wrong with that company...
Share
Share
Share
Share
Share
Share
Share
-
5/5/13 Big-O Algorithm Complexity Cheat Sheet
bigocheatsheet.com 15/15
Reply
Dmitri a day ago
msyelf -> myself
Reply
Wesner Moise a day ago
According to Sedgwick, Delete in a BST is O(sqrt(n)) not O(lg n). See http://www.cs.princeton.edu/co...
Reply
Will iam P. Riley-Land a day ago
This is super.
Share
Share
Share
Page styling via BootstrapComments via DisqusAlgorithm detail via WikipediaBig-O complexity graph via Recursive DesignTable source hosted on GithubMashup via @ericdrowell