Escolar Documentos
Profissional Documentos
Cultura Documentos
BigOAlgorithmComplexityCheatSheet
BigOCheatSheet
Searching
Sorting
DataStructures
Heaps
Graphs
Chart
Comments
3,732
2.2k
8.3k
Like
Tweet
KnowThyComplexities!
Hithere!ThiswebpagecoversthespaceandtimeBigOcomplexitiesofcommonalgorithmsusedinComputerScience.Whenpreparing
fortechnicalinterviewsinthepast,Ifoundmyselfspendinghourscrawlingtheinternetputtingtogetherthebest,average,andworstcase
complexitiesforsearchandsortingalgorithmssothatIwouldn'tbestumpedwhenaskedaboutthem.Overthelastfewyears,I've
interviewedatseveralSiliconValleystartups,andalsosomebiggercompanies,likeYahoo,eBay,LinkedIn,andGoogle,andeachtime
thatIpreparedforaninterview,Ithoughttomyself"Whyohwhyhasn'tsomeonecreatedaniceBigOcheatsheet?".So,tosaveallof
youfinefolksatonoftime,Iwentaheadandcreatedone.Enjoy!
Good Fair Poor
Searching
Algorithm
DataStructure
TimeComplexity
Average
DepthFirstSearch(DFS)
BreadthFirstSearch(BFS)
Binarysearch
Linear(BruteForce)
ShortestpathbyDijkstra,
usingaMinheapaspriorityqueue
ShortestpathbyDijkstra,
usinganunsortedarrayaspriority
queue
ShortestpathbyBellmanFord
Graphof|V|verticesand|E|
edges
Graphof|V|verticesand|E|
edges
Sortedarrayofnelements
Array
Graphwith|V|verticesand|E|
edges
Worst
Space
Complexity
Worst
O(|E|+|V|)
O(|V|)
O(|E|+|V|)
O(|V|)
O(log(n))
O(log(n))
O(1)
O(n)
O(n)
O(1)
O((|V|+|E|)log
|V|)
O((|V|+|E|)log
|V|)
O(|V|)
Graphwith|V|verticesand|E|
edges
O(|V|^2)
O(|V|^2)
O(|V|)
Graphwith|V|verticesand|E|
edges
O(|V||E|)
O(|V||E|)
O(|V|)
MoreCheatSheets
GameofThronesCheatSheet
HTML5CanvasCheatSheet
Sorting
Algorithm
Data
Structure
TimeComplexity
Best
Quicksort Array
http://bigocheatsheet.com/
O(n
log(n))
Average Worst
O(n
log(n))
O(n^2)
WorstCaseAuxiliarySpace
Complexity
Worst
O(n)
1/9
12/4/2014
BigOAlgorithmComplexityCheatSheet
HTML5CanvasCheatSheet
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)
Array
O(n)
O(n^2)
O(n^2)
O(1)
Array
O(n)
O(n^2)
O(n^2)
O(1)
Array
O(n^2)
O(n^2)
O(n^2)
O(1)
Array
O(n+k)
O(n+k)
O(n^2)
O(nk)
Array
O(nk)
O(nk)
O(nk)
O(n+k)
Bubble
Sort
Insertion
Sort
SelectSort
Bucket
Sort
RadixSort
DataStructures
DataStructure
Indexing
BasicArray
O(1)
DynamicArray
O(1)
SinglyLinkedList O(n)
DoublyLinkedList O(n)
SkipList
O(log(n))
HashTable
BinarySearchTree O(log(n))
CartresianTree
BTree
O(log(n))
RedBlackTree
O(log(n))
SplayTree
AVLTree
O(log(n))
TimeComplexity
Average
Search Insertion Deletion Indexing
Worst
Search Insertion Deletion
SpaceComplexity
Worst
O(n)
O(1)
O(n)
O(n)
O(n)
O(n)
O(n)
O(1)
O(n)
O(n)
O(n)
O(n)
O(n)
O(1)
O(1)
O(n)
O(n)
O(1)
O(1)
O(n)
O(n)
O(1)
O(1)
O(n)
O(n)
O(1)
O(1)
O(n)
O(n)
O(n)
O(n)
O(nlog(n))
O(1)
O(n)
O(n)
O(n)
O(n)
O(n)
O(n)
O(n)
O(n)
O(n)
O(n)
O(n)
O(n)
O(1)
O(1)
Heaps
Heaps
TimeComplexity
Heapify FindMax ExtractMax IncreaseKey Insert
LinkedList(sorted)
LinkedList(unsorted)
BinaryHeap
O(n)
BinomialHeap
FibonacciHeap
Delete
Merge
O(1)
O(1)
O(n)
O(n)
O(1)
O(m+n)
O(n)
O(n)
O(1)
O(1)
O(1)
O(1)
O(1)
O(log(n))
O(log(n))
O(log(n)) O(log(n))
O(m+n)
O(log(n)) O(log(n))
O(log(n))
O(log(n)) O(log(n))
O(log(n))
O(1)
O(log(n))*
O(1)*
O(1)
Storage
AddVertex
O(log(n))* O(1)
Graphs
Node/EdgeManagement
Adjacencylist
Incidencelist
Adjacencymatrix
Incidencematrix
O(|V|+|E|)
O(1)
O(1)
O(|V|+|E|)
O(|E|)
O(|V|)
O(|V|+|E|)
O(1)
O(1)
O(|E|)
O(|E|)
O(|E|)
O(|V|^2)
O(|V|^2)
O(1)
O(|V|^2)
O(1)
O(1)
O(|V||E|)
O(|E|)
Notationforasymptoticgrowth
letter
(theta)
bound
upperandlower,tight[1]
http://bigocheatsheet.com/
growth
equal[2]
2/9
12/4/2014
BigOAlgorithmComplexityCheatSheet
(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'sreferredtoasa
tightbound(itmustbeboththeupperandlowerbound).Forexample,analgorithmtakingOmega(nlogn)takesatleastnlogntimebut
hasnoupperlimit.AnalgorithmtakingTheta(nlogn)isfarpreferentialsinceittakesATLEASTnlogn(Omeganlogn)andNOMORE
THANnlogn(BigOnlogn).SO
[2]f(x)=(g(n))meansf(therunningtimeofthealgorithm)growsexactlylikegwhenn(inputsize)getslarger.Inotherwords,the
growthrateoff(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
BigOComplexityChart
Thisinteractivechart,createdbyourfriendsoveratMeteorCharts,showsthenumberofoperations(yaxis)requiredtoobtainaresultas
thenumberofelements(xaxis)increase.O(n!)istheworstcomplexitywhichrequires720operationsforjust6elements,whileO(1)is
thebestcomplexity,whichonlyrequiresaconstantnumberofoperationsforanynumberofelements.
Contributors
Editthesetables!
1. EricRowell
2. QuentinPleple
3. NickDizazzo
4. MichaelAbed
5. AdamForsyth
http://bigocheatsheet.com/
3/9
12/4/2014
BigOAlgorithmComplexityCheatSheet
6. JayEngineer
7. JoshDavis
8. makosblade
9. AlejandroRamirez
10. JoelFriedly
11. RobertBurke
12. DavidDorfman
13. EricLefevreArdant
14. ThomasDybdahlAhle
213Comments
BigOCheatSheet
Login
Share Favorite
SortbyBest
Jointhediscussion
MichaelMitchell 2yearsago
Thisisgreat.Maybeyoucouldincludesomeresources(linkstokhanacademy,moocetc)thatwouldexplaineachofthese
conceptsforpeopletryingtolearnthem.
193
Reply Share
AmandaHarlin>MichaelMitchell 2yearsago
Yes!Please&thankyou
48
Reply Share
CamTyler>MichaelMitchell 2yearsago
Thisexplanationin'plainEnglish'helps:http://stackoverflow.com/quest...
19
Reply Share
ArjanNieuwenhuizen>MichaelMitchell ayearago
HerearethelinksthatIknowof.
#1)http://aduni.org/courses/algor...
#2)http://ocw.mit.edu/courses/ele...
#3)https://www.udacity.com/course...
probablyasgoodormaybebetter#2,butIhavenothadachancetolookatit.
http://ocw.mit.edu/courses/ele...
Sincerely,
Arjan
p.s.
https://www.coursera.org/cours...
Thiscoursehasjustbegunoncoursera(dated1July2013),andlooksverygood.
7
Reply Share
fireheron>ArjanNieuwenhuizen ayearago
ThankyouArjan.Espaeciallythecoursera.orgone)
1
Reply Share
JonRenner 2yearsago
Thisisgod'swork.
70
Reply Share
BlakeJennings 2yearsago
i'mliterallycrying
45
Reply Share
AdamHeinermann ayearago
Isthereaprinterfriendlyversion?
41 Reply Share
http://bigocheatsheet.com/
4/9
12/4/2014
BigOAlgorithmComplexityCheatSheet
41
Reply Share
ThomasFeichtinger>AdamHeinermann 6monthsago
Actuallycopyingthecontentstoagoogledocworkedprettywell!
Ihavemadeitpublic,havealook:
https://docs.google.com/spread...
11
Reply Share
ericdrowell
notyet,butthat'sagreatidea!
6
Reply Share
GokceToykuyu 2yearsago
Couldweaddsometreealgorithmsandcomplexities?Thanks.IreallyliketheRedBlacktrees)
31
Reply Share
ericdrowell
Excellentidea.I'lladdasectionthatcomparesinsertion,deletion,andsearchcomplexitiesforspecificdatastructures
28
Reply Share
DarrenLeRedgatr 2yearsago
Icameherefromanidletwitterclick.Ihavenoideawhatit'stalkingaboutoranyofthecomments.ButIlovethefactthereare
peopleouttherethisclever.Makesmethinkthatonedayhumanitywillcomegood.Cheers.
44
Reply Share
ValentinStanciu 2yearsago
1.Deletion/insertioninasinglelinkedlistisimplementationdependent.Forthequestionof"Here'sapointertoanelement,how
muchdoesittaketodeleteit?",singlelinkedliststakeO(N)sinceyouhavetosearchfortheelementthatpointstotheelement
beingdeleted.Doublelinkedlistssolvethisproblem.
2.Hashescomeinamillionvarieties.HoweverwithagooddistributionfunctiontheyareO(logN)worstcase.Usingadouble
hashingalgorithm,youendupwithaworstcaseofO(loglogN).
3.Fortrees,thetableshouldprobablyalsocontainheapsandthecomplexitiesfortheoperation"GetMinimum".
20
Reply Share
AlexisMas>ValentinStanciu 3monthsago
Ifyoualist:ABCD,WhenyouwanttodeleteB,youcandeleteanodewithoutiteratingoverthelist.
1.B.data=C.data
2.B.next=C.next
3.deleteC
Ifyoucan'tcopydatabetweennodesbecauseitstooexpensivethenyes,it'sO(N)
2
Reply Share
qwertykeyboard 2yearsago
ItwouldbeveryhelpfultohaveexporttoPDF.Thx
16
Reply Share
Gene>qwertykeyboard ayearago
YoucouldconvertthedocumentyourselfusingPandoc:http://johnmacfarlane.net/pand...
Itmighttakeyoualongtimetogetitworking,butPandocisanamazingonestopshopforfileconversion,andcross
platformcompatible.
IfIunderstandbigohnotationcorrectlyImightsay"IestimateyourlearningrateforlearningPandocwillbeO(1).".
2
Reply Share
Ashutosh>Gene 9monthsago
provedO(n),n=numberofformatconversionstolearn:)
2
Reply Share
JuanCarlosAlvarez>Gene 9monthsago
bigoh.hahafunny.
http://bigocheatsheet.com/
Reply Share
5/9
12/4/2014
BigOAlgorithmComplexityCheatSheet
Guest 2yearsago
Finalsarealreadyover...Thisshouldhavebeensharedaweekago!Wouldhavesavedmelike45minutesofusingWikipedia.
10
Reply Share
tempire 2yearsago
Thischartseemsmisleading.BigOisworstcase,notaveragecase~isaveragecase.O(...)shouldn'tbeusedintheaverage
casecolumns.
15
Reply Share
guest>tempire 2yearsago
IthinkbigOisjustanupperbound.Itcouldbeusedforall(best,worstandaverage)cases.AmIwrong?
14
Reply Share
Luis>guest 2yearsago
Youareright.
3
Reply Share
Oleksandr>Luis ayearago
@LuisThatisWRONG.@tempireiscorrect.BigOcannotbeusedforlower,average,andupper
bound..BigO(Omicron)istheWorstCaseScenario.Itistheupperboundforforthealgorithm.For
instanceinalinearsearchalgorithm,worstcaseiswhenthelistiscompletedoutoforder,i.e.thelist
sortedbutbackwards.Omegaisthelowerbound.Thisisalmostpointlesstohave,forinstanceyouwould
ratherhaveBigOthenOmegabecauseitisexactlythesameassay"itwilltakemorethanfivedollarsto
gettoN.Y.vs.Itswillalwaystake,atmost,135dollarstogettoNewYork."Thefirstbitofinformationfrom
Omegaisessentiallyuseless,thethirdhowevergivesyoutheconstraint.Thetaistheupperandlower
boundtogether.Thisisthemostbeneficiallypieceofinformationtohaveaboutanalgorithmbut
unfortunatelyitisusuallyveryhardtofind,butwehavedonethis.Youcanusuallyfindthataverageforan
algorithmsefficiencybytestingitinaveragecaseandworstcasestogether.Simplythisisacomputational
exercisetoextracttheempiricaldata.ThereisanotherproblemIdonotlikeisthecolorschemeis
sometimeswrong..O(n)isbettertheO(log(n))?Inwhatway?1024vs10incrementsthatasortalgorithm
hastoperformforinstance?Allinallthisisgoodinformationbutinitscurrentstate,tothenovice,honestly
itneedstobetakenwithagrainofsaltandfactcheckwithagoodalgorithmbook.However,thisisin
MHOsoifI'moffbaseorincorrectthenfeelfreetoflamemelikethefantasticfouratagayparade:)
4
Reply Share
Guest>Oleksandr ayearago
@OleksandrYouareconfused.Yourexampleaboutthedollarsstatesspecificamounts(e.g."atmost
135dollars"),butbigOandrelatedconceptsareusedtoboundtheorder(linear,exponential,etc.)ofa
functionthatdescribeshowanalgorithmgrows(inspace,time,etc.)withproblemsize.Tobemore
appropriate,yourexampleshouldbemodifiedtosaysomethinglike"ittakesatmost2$permile"(linear).
Withthisinmind,youcanthusunderstandhowbigOcanbeusedbothfor,say,thebestandtheworst
case.Takeyourlinearsearch.Asthesizeoftheproblemgrows(thearraytobesearchedgrowsinsize),
thebestcasestillhasanuppertimeboundofO(1)(ittakesconstanttimetofindanelementinindex0,or
anotherfixedposition),whiletheworstcase(theobjectisinthelastindexwherewelook)hasanupper
timeboundofO(n)(ittakesanumberofstepsoforderequaltotheproblemsize,n,untilwefindtheobject
inthelastindexwherewelook.).
(fixed:wrongautocompleteofwhoIrepliedto)
1
Reply Share
PhilipMachanick>Oleksandr 10monthsago
Omegaisuselessunlessitisatightbound,i.e.,itrepresentsrealminimalcasesthatareinteresting(when
youhaveoptionslike<=or>=inthedefinitionofabound,youshouldatleastgetclosetothe=case,
otherwiseyoumightaswellusethestrictly<or>cases,andeventhereyoushouldtrytofindbounds
thatarereasonablyclosetothe=case).Forexample,strictlyspeaking,quicksortisOmega(1),but
Omega(nlogn)ismoreinformativebecausetellsyouitsrealbestcase.
Inanycase,youdonotnormallyuseOmega,Thetaetc.fordifferentiatingaverage,bestandworstcase.
Theseareboundsonanyofthesecases.Forquicksort,theworstcaseanalysisisn^2andthisisboth
theupperandlowerboundontheworstcase.YouuseOmega,Theta,etc.whentheanalysisfora
particularcaseisnotclearandyouhavetosayitisnobetterthanornoworsethanaparticularanalysis.
http://bigocheatsheet.com/
Reply Share
6/9
12/4/2014
BigOAlgorithmComplexityCheatSheet
BobFoster>Oleksandr 10monthsago
Ibelieveyouarecorrect.Oisworstcase.
Reply Share
Luis>Oleksandr ayearago
@OleksandrYouareconfused.Yourexampleaboutthedollarsstatesspecificamounts(e.g."atmost
135dollars"),butbigOandrelatedconceptsareusedtoboundtheorder(linear,exponential,etc.)ofa
functionthatdescribeshowanalgorithmgrows(inspace,time,etc.)withproblemsize.Tobemore
appropriate,yourexampleshouldbemodifiedtosaysomethinglike"ittakesatmost2$permile"(linear).
Withthisinmind,youcanthusunderstandhowbigOcanbeusedbothfor,say,thebestandtheworst
case.Takeyourlinearsearch.Asthesizeoftheproblemgrows(thearraytobesearchedgrowsinsize),
thebestcasestillhasanuppertimeboundofO(1)(ittakesconstanttimetofindanelementinindex0,or
anotherfixedposition),whiletheworstcase(theobjectisinthelastindexwherewelook)hasanupper
timeboundofO(n)(ittakesanumberofstepsoforderequaltotheproblemsize,n,untilwefindtheobject
inthelastindexwherewelook.).
Reply Share
Oleksandr1>Luis ayearago
Youmakeaverypoorassumptionthatbecauseaspecificvalueisgiven,
thanitmustbealinearfunction.Itisinfactanypolynomialfunction
ofmychoicegivenitsparametersandanyamountofLagrangeconstants
whichwillproduceavalueof135,oranysuchnumberIspecifytobe
usedintheexample.ThepointisthatBigOistheupperboundofa
function.InfactthereareaninfiniteamountofBigO'sforany
elementaryfunctions.BigOcannotbeusedforthebestcasescenario,
thisisacompletemisunderstandingofOmegavsOmicron.Youshouldread
uponthisbecausethisisveryimportant.Asfortheexample,$135
dollarswasgivenasanupperbound,$5wastheOmegavalue,I'mnot
surewhyyoudon'tunderstandaveryclearanalogy,butforyouIchange
situationandvalues.Givenfunctionunknown,itwillrunmorethan
fiveiterations(Omega),BUTitwillneverrunmorethan135iterations.
135beingtheOmicronvalue.Onthelinearsearchalgorithm,forgive
me,ImeanttosayLinearSortAlgorithm,whichhastheworstcan
scenariowhenalistisfedtosaidalgorithminorder,butbackwards.I
agreeaboutwhatyousaidaboutlinearsearchalgorithm.
seemore
Reply Share
YavuzYetim>Oleksandr1 ayearago
@Oleksandr@LuisIMHO,therearethreedifferentstatementsinthisargument,thatleadtotheeventual
misunderstanding.IagreewithLuisthatthetableiscorrectandnotuselessbutalsoagreewithOleksandr
thatit'snotcomplete(butagaindisagreethatitisincompletebecauseofthemismatchbetween
best/averagecaseandbigO,seeStatementiiiandExample(a)intheend).
Themainconfusionisbetweentheterms"case"and"bound".Theseareorthogonalterms,anddonot
haveanyrelationwitheachother.Forexample,youhavealowerboundforaveragecase,orupper
boundforbestcase,...(intotal9different,correctcombinations,eachusefulforadifferentusecase,but
nonehasuseless/meaninglessinformation)
Herearethestatementsinthisargument:
Statementi)"ThetableiswronginusingBigOnotationforallcolumns".Thisstatementisfalsebecause
thetableiscorrect.BigOnotationdoesnothaveanythingtodowiththeworstcase,averagecaseorthe
bestcase.BigOnotationisonlyarepresentationforafunction.Let'ssaythebestcaseruntimeforan
algorithmforagiveninputofsizenisexactly(3*n+1).Onecorrectrepresentationforthisfunctionis
O(n).Therefore,writingO(n)forabestcaseentryiscorrect.
Statementii)"Thebestcaseandaveragecasecolumnsarecorrectindefinitionbut
useless/meaningless".Thisstatementisalsofalse.Whilelearningthe"averagecase"(3*n+1)asO(n)
seemore
ericdrowell
http://bigocheatsheet.com/
Reply Share
7/9
12/4/2014
BigOAlgorithmComplexityCheatSheet
ericdrowell
I'lltrytoclarifythat.Thanks!
1
Reply Share
StphaneDuguay 2yearsago
Hi,I'dliketouseafrenchversionofthispageinclass...shouldItranslateitonanotherwebsiteoryoucansupportlocalisation
andIdothedataenteringforfrench?I'minterested!
11
Reply Share
MartenCzech>StphaneDuguay 2yearsago
learnEnglish!
24
Reply Share
Marcus>MartenCzech ayearago
MaybehemeanshewantstodeliverittoFrenchstudents.Ifheisofferingtodothedataentryfromfrench,but
clearlyspeaksEnglish(fromhiscomment).Don'tbeignorant,thereisnoreasonthateverythingshouldbein
English.
14
Reply Share
MartenCzech>Marcus ayearago
ITworldticksinEnglish,thesoonerFrenchrealizethatthefasterwecangotogether.
6
Reply Share
hey>StphaneDuguay 7monthsago
Voulezvousunetraductionencore?
Reply Share
JonRenner ayearago
AnywayIcangetaPDFversionwithouttakingscreenshotsmyself?
7
Reply Share
sigmaalgebra 2yearsago
Youomittedaninplace,guaranteedO(nlog(n))arraysort,
e.g.,heapsort.Youomittedradixsortthatcanbefaster
thananyofthealgorithmsyoumentioned.
MightmentionSATandrelatedproblemsinNPcomplete
wherethebestknownalgorithmfor
aproblemofsizenhasO(2^n).
Mightincludeanactual,precisedefinitionofO().
7
Reply Share
AntoineGrondin 2yearsago
IthinkDFSandBFS,underSearch,wouldbemoreappropriatelistedasGraphinsteadofTree.
7
Reply Share
ericdrowell
Fixed!Thanks
3
Reply Share
QuentinPlepl>AntoineGrondin 2yearsago
Agreed
Reply Share
AnkushGupta 2yearsago
Awesomeresource!YoushouldaddDijkstrausingaFibonacciHeap!
6
Reply Share
AnonimancioCobardoso 2yearsago
Youcouldincludeachartwithlogarithmicscale.LooksnicerIMHO.
5
Reply Share
http://bigocheatsheet.com/
8/9
12/4/2014
BigOAlgorithmComplexityCheatSheet
GborNdai 2yearsago
Nice.
5
Reply Share
maxw3st 2yearsago
ThisgivesmesomeexcellenthomeworktodoofavarietyI'mnotgettinginclasses.Thankyou.
5
Reply Share
AmitK 2yearsago
Itsprettyhandy!
4
Reply Share
IvanKuckir 2yearsago
Doyoureallyfindthisuseful?
Whentalkingaboutcomplexity,youmusttalkaboutsomespecificalgorithm.Butwhenyouknowthealgorithm,youalready
knowthecomplexity,amIwrong?Doesanybodyjustlearnstheparisalgorithm_name:complexity,withoutanyideahow
algorithmworks?OMG...
6
Reply Share
ericdrowell
haveyouneverhadatechnicalinterviewbefore?
7
Reply Share
PagestylingviaBootstrap
IvanKuckir>ericdrowell 2yearsago
CommentsviaDisqus
No,Iamstillastudent.AndIthink,thatifemployerwantsyoutoknowjustalgorithmcomplexity,butnotthewhole
AlgorithmdetailviaWikipedia
BigOcomplexitychartviaMeteorCharts
TablesourcehostedonGithub
Mashupvia@ericdrowell
http://bigocheatsheet.com/
9/9