4/27/2014
BigO Algorithm ComplexityCheat Sheet
BigO Cheat Sheet
Searching
Sorting
Know Thy Complexities!
Hi there! This webpage covers the space and time BigO 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 myself "Why oh why hasn't someone created a nice BigO cheat sheet?". So, to save all of you fine folks a ton of time, I went ahead and created one. Enjoy!
Algorithm
Data Structure
Time Complexity
Average
Worst
Space
Complexity
Worst
Graph of V edges 
vertices and E 

^{} 
O(E+V) 
O(V)


Graph of V vertices and E edges 

^{} 
O(E+V) 
O(V)


Sorted array of n elements 
O(log(n))
O(n)

O(log(n))
O(n)

O(1)


Array 
O(1)


Graph with V vertices and E edges 
O((V+E)log 
O((V+E)log 
O(V)


V) 
V) 

Graph with V vertices and E edges 
O(V^2)

O(V^2)

O(V)


^{G}^{r}^{a}^{p}^{h} ^{w}^{i}^{t}^{h} ^{}^{V}^{} ^{v}^{e}^{r}^{t}^{i}^{c}^{e}^{s} ^{a}^{n}^{d} ^{}^{E}^{} edges 
O(VE)

O(VE)

O(V)


Sorting 

Algorithm 
Data Structure 
Time Complexity 
Worst Case Auxiliary Space Complexity 
Best
Average
Worst
Worst
http://bigocheatsheet.com/
1/10
4/27/2014
BigO Algorithm ComplexityCheat Sheet
Array 

Array 

Array 

Array 
Insertion Sort Array
Array 

Array 

Array 
Data Structures
Data Structure
Basic Array Dynamic Array SinglyLinked List DoublyLinked List Skip List Hash Table Binary Search Tree Cartresian Tree BTree RedBlack Tree Splay Tree AVL Tree
Heaps
http://bigocheatsheet.com/
2/10
4/27/2014
BigO Algorithm ComplexityCheat Sheet
Notation for asymptotic growth
letter 
bound 
growth 
(theta) Θ 
upper and lower, tight ^{[}^{1}^{]} 
equal ^{[}^{2}^{]} 
(bigoh) O upper, tightness unknown _{l}_{e}_{s}_{s} _{t}_{h}_{a}_{n} _{o}_{r} _{e}_{q}_{u}_{a}_{l} [3]
(smalloh) 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 be both 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). ^{S}^{O}
[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) is asymptotically proportional to g(n).
[3] Same thing. Here the growth rate is no faster than g(n). bigoh is the most useful because represents the worstcase behavior.
In short, if algorithm is algorithm performance
o(n) 
< n 
O(n) 
≤ n 
Θ(n) 
= n 
Ω(n) 
≥ n 
ω(n) 
> n 
then its performance is
BigO Complexity Chart
This interactive chart, created by our friends over at MeteorCharts, shows the number of operations (y axis) required to obtain a result as the number of elements (x axis) increase. O(n!) is the worst complexity which requires 720 operations for just 6 elements, while O(1) is the best complexity, which only requires a constant number of operations for any number of elements.
http://bigocheatsheet.com/
3/10
4/27/2014
BigO Algorithm ComplexityCheat Sheet
Contributors
1. 

2. 

3. 

4. 

5. 

6. 

7. 

8. 

9. 

10. 

11. 

12. 

13. 

14. 
185 Comments
BigO Cheat Sheet
Login
Sort by Best
Share
Favorite
Join the discussion…
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.
Michael Mitchell • a year ago
157
• Reply
•
Share ›
http://bigocheatsheet.com/
4/10
4/27/2014
BigO Algorithm ComplexityCheat Sheet
Amanda Harlin
Michael Mitchell •
Yes! Please & thank you
38
• Reply •
Share ›
a year ago
This explanation in 'plain English' helps: http://stackoverflow.com/quest
Cam Tyler
Michael Mitchell •
11 months ago
16
• Reply •
Share ›
Arjan Nieuwenhuizen
Michael Mitchell •
Here are the links that I know of.
10 months ago
#1) http://aduni.org/courses/algor #2) http://ocw.mit.edu/courses/ele #3) https://www.udacity.com/course
probably as good or maybe better # 2, but I have not had a chance to look at it. http://ocw.mit.edu/courses/ele
Sincerely,
Arjan
p.s. https://www.coursera.org/cours This course has just begun on coursera (dated 1 July 2013), and looks very good.
Share ›
Arjan Nieuwenhuizen •
7 months ago
Thank you Arjan. Espaecially the coursera.org one ;)
• Reply
•
Share ›

Adam Heinermann • 11 months ago 

Is there a printerfriendly version? 

39 


• 
Reply • Share › 


ericdrowell 
Mod
Adam Heinermann 
• 
4 months ago 

not yet, but that's a great idea! 

5 


• 
Reply • 
Share › 


Gokce Toykuyu • a year ago 

Could we add some tree algorithms and complexities? Thanks. I really like the RedBlack trees ;) 

31 


• 
Reply • Share › 


ericdrowell 
Mod

Gokce Toykuyu • a year ago 

Excellent idea. I'll add a section that compares insertion, deletion, and search complexities for specific data structures 

24 


• Reply • 
Share › 


Jon Renner • a year ago 

This is god's work. 

44 


• 
Reply • Share › 


Blake Jennings • 11 months ago 

i'm literally crying 

23 


• 
Reply • Share › 


Darren Le Redgatr • a year ago 

I came here from an idle twitter click. I have no idea what it's talking about or any of the comments. But I love the fact there are 

eo le out there this clever Makes me think that one da 
humanit 
will come 
ood Cheers 
http://bigocheatsheet.com/
5/10
4/27/2014
BigO Algorithm ComplexityCheat Sheet
p
31
p
• Reply •
.
Share ›
y
y
g
.
.
Valentin Stanciu • a year 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?", singlelinked lists take O(N) since you have to search for the element that points to the element being deleted. Doublelinked 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".
18
• Reply •
Share ›
qwertykeyboard • a year ago
• Reply •
Gene
Share ›
It would be very helpful to have export to PDF. Thx
6 months ago
qwertykeyboard •
You could convert the document yourself using Pandoc: http://johnmacfarlane.net/pand It might take you a long time to get it working, but Pandoc is an amazing one stop shop for file conversion, and cross platform compatible. If I understand big oh notation correctly I might say "I estimate your learning rate for learning Pandoc will be O(1). ".
2
• Reply
•
Share ›
Ashutosh
Gene •
2 months ago
proved O(n), n=number of format conversions to learn :)
• Reply •
Share ›
Juan Carlos Alvarez
big oh. haha funny.
Gene •
tempire •
• Reply •
Share ›
2 months ago
This chart seems misleading. Big O is worst case, not average case; ~ is average case. O( case columns.
)
shouldn't be used in the average
• Reply •
Share ›
guest
tempire •
11 months ago
I think big O is just an upper bound. It could be used for all (best, worst and average) cases. Am I wrong?
6 months ago
@Luis That is WRONG. @tempire is correct. Big O cannot be used for lower, average, and upper
bound
instance in a linear search algorithm, worst case is when the list is completed out of order, i.e. the list sorted but backwards. Omega is the lower bound. This is almost pointless to have, for instance you would rather have Big O then Omega because it is exactly the same as say "it will take more than five dollars to
get to N.Y. vs. Its will always take, at most, 135 dollars to get to New York." The first bit of information from Omega is essentially useless, the third however gives you the constraint. Theta is the upper and lower bound together. This is the most beneficially piece of information to have about an algorithm but unfortunately it is usually very hard to find, but we have done this. You can usually find that average for an algorithms efficiency by testing it in average case and worst cases together. Simply this is a computational exercise to extract the empirical data. There is another problem I do not like is the color scheme is
sometimes wrong
Big O (Omicron) is the Worst Case Scenario. It is the upper bound for for the algorithm. For
O(n) is better the O(log(n))? In what way? 1024 vs 10 increments that a sort algorithm
http://bigocheatsheet.com/
6/10
4/27/2014
BigO Algorithm ComplexityCheat Sheet
has to perform for instance? All in all this is good information but in its current state, to the novice, honestly it needs to be taken with a grain of salt and fact check with a good algorithm book. However, this is in MHO so if I'm off base or incorrect then feel free to flame me like the fantastic four at a gay parade :)
2
• Reply •
Oleksandr •
Share ›
6 months ago
Guest
@Oleksandr You are confused. Your example about the dollars states specific amounts (e.g. " at most
135 dollars"), but big O and related concepts are used to bound the order (linear, exponential, etc.) of a
function that describes how an algorithm grows (in space, time, etc.) with problem size. To be more appropriate, your example should be modified to say something like "it takes at most 2$ per mile" (linear). With this in mind, you can thus understand how big O can be used both for, say, the best and the worst case. Take your linear search. As the size of the problem grows (the array to be searched grows in size), the best case still has an upper time bound of O(1) (it takes constant time to find an element in index 0, or another fixed position), while the worst case (the object is in the last index where we look) has an upper time bound of O(n) (it takes a number of steps of order equal to the problem size, n, until we find the object in the last index where we look.).
(fixed: wrong autocomplete of who I replied to)
1
• Reply •
Share ›
Omega is useless unless it is a tight bound, i.e., it represents real minimal cases that are interesting (when you have options like <= or >= in the definition of a bound, you should at least get close to the = case, otherwise you might as well use the strictly < or > cases, and even there you should try to find bounds that are reasonably close to the = case). For example, strictly speaking, quicksort is Omega(1), but Omega(n log n) is more informative because tells you its real best case.
Philip Machanick
Oleksandr •
3 months ago
In any case, you do not normally use Omega, Theta etc. for differentiating average, best and worst case. These are bounds on any of these cases. For quicksort, the worst case analysis is n^2 and this is both the upper and lower bound on the worst case. You use Omega, Theta, etc. when the analysis for a particular case is not clear and you have to say it is no better than or no worse than a particular analysis.
135 dollars"), but big O and related concepts are used to bound the order (linear, exponential, etc.) of a
function that describes how an algorithm grows (in space, time, etc.) with problem size. To be more appropriate, your example should be modified to say something like "it takes at most 2$ per mile" (linear). With this in mind, you can thus understand how big O can be used both for, say, the best and the worst case. Take your linear search. As the size of the problem grows (the array to be searched grows in size), the best case still has an upper time bound of O(1) (it takes constant time to find an element in index 0, or another fixed position), while the worst case (the object is in the last index where we look) has an upper time bound of O(n) (it takes a number of steps of order equal to the problem size, n, until we find the object in the last index where we look.).
• Reply •
Share ›
You make a very poor assumption that because a specific value is given, than it must be a linear function. It is in fact any polynomial function of my choice given its parameters and any amount of Lagrange constants which will produce a value of 135, or any such number I specify to be used in the example. The point is that Big O is the upper bound of a function. In fact there are an infinite amount of Big O's for any elementary functions. Big O cannot be used for the best case scenario,
Oleksandr1
Luis
•
6 months ago
http://bigocheatsheet.com/
7/10
4/27/2014
BigO Algorithm ComplexityCheat Sheet
this is a complete misunderstanding of Omega vs Omicron. You should read up on this because this is very important. As for the example, $135 dollars was given as an upper bound, $5 was the Omega value, I'm not sure why you don't understand a very clear analogy, but for you I change situation and values. Given function unknown, it will run more than five iterations (Omega), BUT it will never run more than 135 iterations. 135 being the Omicron value. On the linear search algorithm, forgive me, I meant to say Linear Sort Algorithm, which has the worst can scenario when a list is fed to said algorithm in order, but backwards. I agree about what you said about linear search algorithm.
• Reply •
Share ›
see more
Yavuz Yetim
Oleksandr1
•
6 months ago
@Oleksandr @Luis IMHO, there are three different statements in this argument, that lead to the eventual misunderstanding. I agree with Luis that the table is correct and not useless but also agree with Oleksandr that it's not complete (but again disagree that it is incomplete because of the mismatch between best/average case and bigO, see Statement iii and Example (a) in the end).
The main confusion is between the terms "case" and "bound". These are orthogonal terms, and do not
have any relation with each other. For example, you have a lowerbound for averagecase, or upperbound
for bestcase,
 has useless/meaningless information)
(in total 9 different, correct combinations, each useful for a different use case, but none
Here are the statements in this argument:
Statement i) "The table is wrong in using BigO notation for all columns". This statement is false because the table is correct. BigO notation does not have anything to do with the worst case, average case or the best case. BigO notation is only a representation for a function. Let's say the bestcase run time for an algorithm for a given input of size n is exactly (3*n + 1). One correct representation for this function is O(n). Therefore, writing O(n) for a bestcase entry is correct.
Statement ii) "The bestcase and averagecase columns are correct in definition but useless/meaningless" This statement is also false While learning the "average case" (3*n + 1) as O(n)
I'll try to clarify that. Thanks!
1
• Reply
•
Share ›
see more

Guest • 
a year ago 

Finals are already over This should have been shared a week ago! Would have saved me like 45 minutes of using Wikipedia. 

9 


• Reply 
• Share › 


Stéphane Duguay • a year ago 

Hi, I'd like to use a french version of this page in class and I do the data entering for french? I'm interested! 
should I translate it on another website or you can support localisation 
http://bigocheatsheet.com/
8/10
4/27/2014
BigO Algorithm ComplexityCheat Sheet
Jon Renner •
English.
11
• Reply
•
Marten Czech
•
7 months ago
IT world ticks in English, the sooner French realize that the faster we can go together.
• Reply
•
Share ›
Anyway I can get a PDF version without taking screenshots myself?
7
• Reply •
Share ›
sigmaalgebra • a year ago
You omitted an inplace, 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.
Might mention SAT and related problems in NPcomplete
where the best known algorithm for a problem of size n has O(2^n).
Might include an actual, precise definition of O().
7
• Reply •
Share ›
Antoine Grondin • a year ago
I think DFS and BFS, under Search, would be more appropriate listed as Graph instead of Tree.
7
• Reply •
Share ›
ericdrowell
Fixed! Thanks
3
• Reply
•
Antoine Grondin •
Share ›
a year ago
Agreed
Quentin Pleplé
• Reply
•
Antoine Grondin •
Share ›
a year ago
Ankush Gupta • 11 months ago
Awesome resource! You should add Dijkstra using a Fibonacci Heap!
5
• Reply •
Share ›
Anonimancio Cobardoso • a year ago
You could include a chart with logarithmic scale. Looks nicer IMHO.
5
• Reply •
Share ›
Gábor Nádai • a year ago
This gives me some excellent homework to do of a variety I'm not getting in classes. Thank you.
4 


• 
Reply • 
Share › 
AmitK • a year ago 

Its pretty handy! 

3 


• 
Reply • 
Share › 
IvanKuckir • a year ago http://bigocheatsheet.com/
9/10
4/27/2014
BigO Algorithm ComplexityCheat Sheet
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
•
Share ›
ericdrowell
IvanKuckir •
a year ago
have you never had a technical interview before?
• Reply
IvanKuckir
a year ago
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
1
• Reply
•
Share ›
That's too strong. There are simply too many algorithms. Also, just because certain companies are asking algorithms, this fact does not imply other companies have a lower expectation. Most of the top companies I know of don't even go with RedBlack tree. Both of them are interested in basic tree/graph and sorting algorithms and give you one or two puzzles that don't really help in real life. Half of the Google interview questions are good, but the other half are puzzles that I find (and certainly a lot of people) less helpful . One I find useful one is fitting GBs of data into 1M memory if I remember correctly.
mrtvb
IvanKuckir •
a year ago
Also, not everyone will remember the complexity. Certain people will never use algorithms above tree search or sorting. They might not even need streaming algorithm.
• Reply
•
IvanKuckir •
Share ›
4 months ago
Page styling via Bootstrap Comments via Disqus Algorithm detail via Wikipedia BigO complexity chart via MeteorCharts Table source hosted on Github Mashup via @ericdrowell
http://bigocheatsheet.com/
10/10
Molto più che documenti.
Scopri tutto ciò che Scribd ha da offrire, inclusi libri e audiolibri dei maggiori editori.
Annulla in qualsiasi momento.