TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Big-O Algorithm Complexity Cheat Sheet

520 pointsby ashleyblackmoreabout 12 years ago

29 comments

wtingabout 12 years ago
You can pass some interviews by blindly memorizing, but it's unnecessary. If you understand a concept, then you can reason its big O. Memorization implies a superficial understanding that may be revealed later.<p>If you don't understand something, spend a few hours and implement it.<p><pre><code> "I hear and I forget. I see and I remember. I do and I understand." - Confucious</code></pre>
评论 #5655446 未加载
评论 #5656102 未加载
评论 #5656894 未加载
评论 #5655228 未加载
评论 #5655346 未加载
评论 #5655507 未加载
评论 #5655484 未加载
评论 #5655532 未加载
algoriasabout 12 years ago
This is why it's a terrible idea to ask for some random algorithm runtime in an interview. It says absolutely nothing about programming or reasoning skill.<p>I'd rather ask a candidate to explain their favorite data structure to me and derive it's big-O complexity right then and there. Being able to regurgitate the correct answer doesn't count for anything in my book.
评论 #5655263 未加载
评论 #5656825 未加载
vault_about 12 years ago
This is a pretty limited list of algorithms. It should definitely include linear time sorting algorithms (e.g. bucket or radix sort), as well as graph algorithms (shortest path at least, but also probably all pair shortest path and minimum spanning tree).<p>There should also be a section about heaps and their operations. There are a huge number of ways to implement a heap (e.g. linked list, binary tree, or a more exotic structure like a binomial of fibonacci heap) and there are a lot of tradeoffs in complexity of different operations between them.
评论 #5655647 未加载
评论 #5655384 未加载
评论 #5655225 未加载
anonymouzabout 12 years ago
I suppose the general idea for the colors is something like: green = best in category, red = worst in category, yellow = neither best nor worst?<p>In that case bubble sort and insertion sort should be green for the best-case time complexity ( O(n) vs. O(n log(n)) for quicksort/mergesort).<p>It might also be interesting to make the plot dynamic and allow the visitor to play with different implicit constants for the individual asymptotic bounds.
评论 #5655301 未加载
评论 #5655651 未加载
评论 #5657414 未加载
wfunctionabout 12 years ago
If you need this, you're doing yourself a disservice by looking at it.<p>Go back and learn the concepts so that you're not memorizing anything.
评论 #5656410 未加载
alecbenzerabout 12 years ago
I'm not totally sure what you mean by "dynamic array", but the vector algorithm for insertion (which you should probably at least include, if by "dynamic array" you were implying a more naive insertion scheme) is O(1) amortized.
评论 #5655323 未加载
Retricabout 12 years ago
I never understood why people look at 7-8 sorting methiods and ignore Radix sort which often beats everything else at O(n) average case. <a href="https://en.wikipedia.org/wiki/Radix_sort" rel="nofollow">https://en.wikipedia.org/wiki/Radix_sort</a> I mean is the assumption that people would never actually need a useful real world understanding of the topic?
评论 #5655722 未加载
评论 #5655894 未加载
calebeggabout 12 years ago
Why is O(n) red and O(n log(n)) yellow? Clearly, O(n log(n)) is slower.<p>In general, whether a specific complexity is good or bad differs greatly based on what you're doing. I don't think it's a good idea to have the colors be the same everywhere. A particularly bad instance is how every single data structure is O(n), which is red/"bad".
Jabblesabout 12 years ago
Very few commenters think this is a good idea. The majority of posts lament the rote learning and lack of understanding involved. Why then, is this upvoted so much? Is it that people think the comments are worth reading so much that they upvote the article in the hope that other readers will read the comments? Are the people commenting negatively upvoting the article in the hopes their comments will be more widely read†? Are people afraid of flagging articles?<p>†testable hypothesis, data requested
评论 #5655991 未加载
评论 #5656305 未加载
评论 #5656379 未加载
coldcodeabout 12 years ago
If you only want to hire me based on answering big-O questions I don't want to work there anyway. 32 years of working on highly complex and performant stuff and not once did I think in terms of big-0. Optimizing is not about knowing the math but knowing how to measure and how to interpret what you measure. Big-O might make you feel smart but it's a tiny part of actually constructing something complex and optimal.
评论 #5660828 未加载
omershapiraabout 12 years ago
Slightly academic, but this cheat sheet gives out some shorthand explanations to many of the methods in the Big-O document:<p><a href="http://www.scribd.com/doc/39557873/Data-Structures-Cheat-Sheet" rel="nofollow">http://www.scribd.com/doc/39557873/Data-Structures-Cheat-She...</a>
评论 #5655226 未加载
westurnerabout 12 years ago
* <a href="https://en.wikipedia.org/wiki/Computational_complexity_theory" rel="nofollow">https://en.wikipedia.org/wiki/Computational_complexity_theor...</a><p>* <a href="http://mathworld.wolfram.com/ComplexityTheory.html" rel="nofollow">http://mathworld.wolfram.com/ComplexityTheory.html</a><p>* <a href="https://complexityzoo.uwaterloo.ca/Zoo_Glossary#O" rel="nofollow">https://complexityzoo.uwaterloo.ca/Zoo_Glossary#O</a><p>* <a href="http://dbpedia.org/resource/Depth-first_search" rel="nofollow">http://dbpedia.org/resource/Depth-first_search</a><p>* <a href="http://en.wikipedia.org/wiki/Category:Infobox_templates" rel="nofollow">http://en.wikipedia.org/wiki/Category:Infobox_templates</a><p>* <a href="http://www.w3.org/TR/rdf-schema/" rel="nofollow">http://www.w3.org/TR/rdf-schema/</a>
lettergramabout 12 years ago
Actually walking through each problem is the only way to understand. There's not even a point in memorizing, it takes longer to memorize than to actually learn how to come up with those values.
pbiggarabout 12 years ago
Can you annotate it with the subtype of the algorithm. For example, you have O(logN) worst-case space complexity for quicksort - that is not true on all variations, include to naive one.
andreasvcabout 12 years ago
<a href="http://webcache.googleusercontent.com/search?q=cache:PPvTs45TpbEJ:bigocheatsheet.com/" rel="nofollow">http://webcache.googleusercontent.com/search?q=cache:PPvTs45...</a>
tlarkworthyabout 12 years ago
If you want to visualise big O runtime, you draw it on a log-log scale. The linear gradient on the log-log plot is the factor. i.e. if its at 45 degrees its O(n), if its at a gradient of 2:1 its O(n^2). Handy fact to work out your big O without having to do the tedious math! (See <a href="http://jcsites.juniata.edu/faculty/kruse/cs2/ch12a.htm" rel="nofollow">http://jcsites.juniata.edu/faculty/kruse/cs2/ch12a.htm</a>)
评论 #5665402 未加载
评论 #5657701 未加载
dmeadabout 12 years ago
not once is theta or omega used, so this cheat sheet isn't all that descriptive.
评论 #5655936 未加载
fmax30about 12 years ago
I don't know why people don't use balanced BST ( std::map in c++) for storing the adjacency lists of a graph. Sure the insertion would take O(log n) time but , I think the overall benefit would be greater than the costs. Correct me if I am wrong.
评论 #5656944 未加载
评论 #5655501 未加载
评论 #5655270 未加载
montecarlabout 12 years ago
I would like to see the same type of complexity cheat sheet for algorithms for common math problems: addition, multiplication, division, subtraction, factorization, solving linear systems, solving eigen systems, matrix inversion, etc.
评论 #5656926 未加载
kriroabout 12 years ago
Pretty cool, thanks.<p>I think the graph at the end is the most useful thing. Really helps understanding the complexity in relative terms.<p>Adding tree stuff would be cool (especially for search which is often implemented as either a tree or graph version)
fatemayasmineabout 12 years ago
It is smart how you can edit the table and contribute via Github.
mmanfrinabout 12 years ago
Question: I'm a junior software developer that did not get a CS degree. What would be the best way to learn and understand this sort of stuff? Coursera/Khan? A book?
评论 #5655542 未加载
评论 #5655523 未加载
评论 #5655988 未加载
评论 #5655519 未加载
zukhanabout 12 years ago
You should create an empty version of the cheat sheet so people can test their understanding of various data structures and algorithms.
Dylan16807about 12 years ago
I'm amused that every single data structure is proportional in size to the amount of data stored and therefore marked as red.
graupabout 12 years ago
Well done!<p>As others pointed out, one might be better off really understanding them, but for a quick overview this is a very usable website.
AYBABTMEabout 12 years ago
This is very shallow and of limited use.
评论 #5655269 未加载
评论 #5655659 未加载
anonymoushnabout 12 years ago
Why don't your sorted trees support indexing? Mine support indexing in O(lg n) time.
oakazabout 12 years ago
Is this only for interviews?
exabrialabout 12 years ago
/trolling on<p>Awesome. Next time I'm trying to pass some silicon valley interview, I'll have to look this up. Till then, I think I have more practical problems to worry about.<p>/trolling off