Vous êtes sur la page 1sur 4

8/13/14 Big-O Algorithm Complexity Cheat Sheet

bigocheatsheet.com 1/10
I receive
$4.69 / wk
on Gittip.
BigOCheatSheet
Searching
Sorting
DataStructures
Heaps
Graphs
Chart
Comments
Tweet Tweet
3,576

2.1k

KnowThyComplexities!
Hithere!ThiswebpagecoversthespaceandtimeBigOcomplexitiesofcommonalgorithmsusedinComputerScience.Whenpreparing
fortechnicalinterviewsinthepast,Ifoundmyselfspendinghourscrawlingtheinternetputtingtogetherthebest,average,andworstcase
complexitiesforsearchandsortingalgorithmssothatIwouldn'tbestumpedwhenaskedaboutthem.Overthelastfewyears,I've
interviewedatseveralSiliconValleystartups,andalsosomebiggercompanies,likeYahoo,eBay,LinkedIn,andGoogle,andeachtimethat
Ipreparedforaninterview,Ithoughttomyself"Whyohwhyhasn'tsomeonecreatedaniceBigOcheatsheet?".So,tosaveallofyoufine
folksatonoftime,Iwentaheadandcreatedone.Enjoy!
Good Fair Poor
Searching
Algorithm DataStructure TimeComplexity
Space
Complexity
Average Worst Worst
DepthFirstSearch(DFS)
Graphof|V|verticesand|E|
edges
- O(|E| + |V|) O(|V|)
BreadthFirstSearch(BFS)
Graphof|V|verticesand|E|
edges
- O(|E| + |V|) O(|V|)
Binarysearch Sortedarrayofnelements O(log(n)) O(log(n)) O(1)
Linear(BruteForce) Array O(n) O(n) O(1)
ShortestpathbyDijkstra,
usingaMinheapaspriorityqueue
Graphwith|V|verticesand|E|
edges
O((|V| + |E|) log
|V|)
O((|V| + |E|) log
|V|)
O(|V|)
ShortestpathbyDijkstra,
usinganunsortedarrayaspriority
queue
Graphwith|V|verticesand|E|
edges
O(|V|^2) O(|V|^2) O(|V|)
ShortestpathbyBellmanFord
Graphwith|V|verticesand|E|
edges
O(|V||E|) O(|V||E|) O(|V|)
Free Cloud Computing
softlayer.com/cloud-computing
Try Our Cloud Solutions for Free. No Charge, Act Quickly, Chat Now!
7.8k
Like Like
8/13/14 Big-O Algorithm Complexity Cheat Sheet
bigocheatsheet.com 2/10
MoreCheatSheets
Sorting
Algorithm
Data
Structure
TimeComplexity
WorstCaseAuxiliarySpace
Complexity
Best Average Worst Worst
Quicksort Array
O(n
log(n))
O(n
log(n))
O(n^2) O(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)
SelectSort 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)
RadixSort Array O(nk) O(nk) O(nk) O(n+k)
DataStructures
DataStructure TimeComplexity SpaceComplexity
Average Worst Worst
Indexing Search Insertion Deletion Indexing Search Insertion Deletion
BasicArray O(1) O(n) - - O(1) O(n) - - O(n)
DynamicArray O(1) O(n) O(n) O(n) O(1) O(n) O(n) O(n) O(n)
SinglyLinkedList O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)
DoublyLinkedList O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)
SkipList O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n) O(n) O(n) O(n) O(n log(n))
HashTable - O(1) O(1) O(1) - O(n) O(n) O(n) O(n)
BinarySearchTree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n) O(n) O(n) O(n) O(n)
CartresianTree
- O(log(n)) O(log(n)) O(log(n)) - O(n) O(n) O(n) O(n)
BTree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)
RedBlackTree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)
SplayTree - O(log(n)) O(log(n)) O(log(n)) - O(log(n)) O(log(n)) O(log(n)) O(n)
AVLTree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)
Heaps
GameofThronesCheatSheet HTML5CanvasCheatSheet
HTML5CanvasCheatSheet
8/13/14 Big-O Algorithm Complexity Cheat Sheet
bigocheatsheet.com 3/10
Heaps TimeComplexity
Heapify FindMax ExtractMax IncreaseKey Insert Delete Merge
LinkedList(sorted) - O(1) O(1) O(n) O(n) O(1) O(m+n)
LinkedList(unsorted) - O(n) O(n) O(1) O(1) O(1) O(1)
BinaryHeap O(n) O(1) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(m+n)
BinomialHeap - O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n))
FibonacciHeap - O(1) O(log(n))* O(1)* O(1) O(log(n))* O(1)
Graphs
Node/EdgeManagement Storage AddVertex AddEdge RemoveVertex RemoveEdge Query
Adjacencylist O(|V|+|E|) O(1) O(1) O(|V| + |E|) O(|E|) O(|V|)
Incidencelist O(|V|+|E|) O(1) O(1) O(|E|) O(|E|) O(|E|)
Adjacencymatrix O(|V|^2) O(|V|^2) O(1) O(|V|^2) O(1) O(1)
Incidencematrix O(|V| |E|) O(|V| |E|) O(|V| |E|) O(|V| |E|) O(|V| |E|) O(|E|)
Notationforasymptoticgrowth
letter bound growth
(theta)
upperandlower,tight
[1]
equal
[2]
(bigoh)O upper,tightnessunknown
lessthanorequal
[3]
(smalloh)o upper,nottight lessthan
(bigomega) lower,tightnessunknown greaterthanorequal
(smallomega) lower,nottight greaterthan
[1]BigOistheupperbound,whileOmegaisthelowerbound.ThetarequiresbothBigOandOmega,sothat'swhyit'sreferredtoasatight
bound(itmustbeboththeupperandlowerbound).Forexample,analgorithmtakingOmega(nlogn)takesatleastnlogntimebuthasno
upperlimit.AnalgorithmtakingTheta(nlogn)isfarpreferentialsinceittakesATLEASTnlogn(Omeganlogn)andNOMORETHAN
nlogn(BigOnlogn).
SO
[2]f(x)=(g(n))meansf(therunningtimeofthealgorithm)growsexactlylikegwhenn(inputsize)getslarger.Inotherwords,thegrowth
rateoff(x)isasymptoticallyproportionaltog(n).
[3]Samething.Herethegrowthrateisnofasterthang(n).bigohisthemostusefulbecauserepresentstheworstcasebehavior.
Inshort,ifalgorithmis__thenitsperformanceis__
algorithm performance
o(n) <n
O(n) n
(n) =n
(n) n
(n) >n
8/13/14 Big-O Algorithm Complexity Cheat Sheet
bigocheatsheet.com 4/10
BigOComplexityChart
Thisinteractivechart,createdbyourfriendsoveratMeteorCharts,showsthenumberofoperations(yaxis)requiredtoobtainaresultasthe
numberofelements(xaxis)increase.O(n!)istheworstcomplexitywhichrequires720operationsforjust6elements,whileO(1)isthebest
complexity,whichonlyrequiresaconstantnumberofoperationsforanynumberofelements.
Contributors
Editthesetables!
1. EricRowell
2. QuentinPleple
3. NickDizazzo
4. MichaelAbed
5. AdamForsyth
6. JayEngineer
7. JoshDavis
8. makosblade
9. AlejandroRamirez
10. JoelFriedly
11. RobertBurke
12. DavidDorfman
13. EricLefevreArdant
14. ThomasDybdahlAhle
202Comments BigOCheatSheet Lo
SortbyBest Share Favorite