You are on page 1of 10

10/11/15

Big-O Algorithm Complexity Cheat Sheet

BigOCheatSheet
DataStructures
Sorting
Graphs
Heaps
Chart
Comments

4,451

2.6k

10k
Like

Tweet

KnowThyComplexities!
Hithere!ThiswebpagecoversthespaceandtimeBigOcomplexitiesofcommonalgorithmsusedinComputerScience.
Whenpreparingfortechnicalinterviewsinthepast,Ifoundmyselfspendinghourscrawlingtheinternetputtingtogether
thebest,average,andworstcasecomplexitiesforsearchandsortingalgorithmssothatIwouldn'tbestumpedwhenasked
aboutthem.Overthelastfewyears,I'veinterviewedatseveralSiliconValleystartups,andalsosomebiggercompanies,
likeYahoo,eBay,LinkedIn,andGoogle,andeachtimethatIpreparedforaninterview,Ithoughttomyself"Whyhasn't
someonecreatedaniceBigOcheatsheet?".So,tosaveallofyoufinefolksatonoftime,Iwentaheadandcreatedone.
Enjoy!Eric

Legend
ExcellentGoodFairBadHorrible

DataStructureOperations
Data
Structure
Access
Array
Stack
Singly
Linked
List
Doubly
Linked
List

Space
Complexity
Worst

TimeComplexity
Average
Search
Insertion

Deletion

Access

Worst
Search
Insertion

Deletion

O(1)

O(n)

O(n)

O(n)

O(1)

O(n)

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(n)

O(1)

O(1)

O(n)

O(n)

O(1)

O(1)

O(n)

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)

O(n)

O(n)

Hash
Table
Binary

bigocheatsheet.com

O(1)

O(1)

O(1)

O(n
log(n))
O(n)

1/10

10/11/15

Big-O Algorithm Complexity Cheat Sheet

Binary
Search
Tree

O(log(n))O(log(n))O(log(n))O(log(n))O(n)

Cartesian
Tree

O(log(n))O(log(n))O(log(n))-

O(n)

O(n)

O(n)

O(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)

Red
Black
Tree

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)

Splay
Tree
AVL
Tree

O(log(n))O(log(n))O(log(n))-

O(log(n))O(log(n))O(log(n))O(n)

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)

ArraySortingAlgorithms
Algorithm

TimeComplexity
Best

Average

SpaceComplexity
Worst

Worst

Quicksort

O(n log(n))O(n log(n))

O(n^2)

O(log(n))

Mergesort
Timsort

O(n log(n))O(n log(n))

O(n log(n))

O(n)

O(n)

O(n log(n))

O(n log(n))

O(n)

Heapsort
O(n log(n))O(n log(n))
BubbleSort O(n)
O(n^2)
InsertionSort O(n)
O(n^2)

O(n log(n))

O(1)

O(n^2)

O(1)

O(n^2)

O(1)

SelectionSort O(n^2)
ShellSort
O(n)

O(n^2)

O(n^2)

O(1)

BucketSort
RadixSort

O(n+k)

O(n+k)

O(n^2)

O(n)

O(nk)

O(nk)

O(nk)

O(n+k)

O((nlog(n))^2)O((nlog(n))^2)O(1)

GraphOperations
Node/EdgeManagement
Storage
Adjacencylist
O(|V|+|E|)
Incidencelist
Adjacencymatrix
Incidencematrix

AddVertex

AddEdge

RemoveVertex RemoveEdge Query

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(|V| |E|)O(|V| |E|)O(|V| |E|) O(|V| |E|)O(|E|)

HeapOperations
Type

TimeComplexity
Heapify FindMax ExtractMax IncreaseKey Insert

Delete

Merge

LinkedList(sorted) LinkedList(unsorted) -

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)

BinaryHeap
BinomialHeap

O(n)

O(1)

O(log(n)) O(log(n))

O(log(n))O(log(n))O(m+n)

O(1)

O(log(n)) O(log(n))

O(1)

O(log(n))O(log(n))

FibonacciHeap

O(1)

O(log(n)) O(1)

O(1)

O(log(n))O(1)

bigocheatsheet.com

2/10

10/11/15

Big-O Algorithm Complexity Cheat Sheet

BigOComplexityChart

RecommendedReading
CrackingtheCodingInterview:150ProgrammingQuestionsandSolutions
IntroductiontoAlgorithms,3rdEdition
DataStructuresandAlgorithmsinJava(2ndEdition)
HighPerformanceJavaScript(BuildFasterWebApplicationInterfaces)

Contributors
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

EricRowell,founderofCoderLifestyle
QuentinPleple
MichaelAbed
NickDizazzo
AdamForsyth
DavidDorfman
JayEngineer
JenniferHamon
JoshDavis
NodirTurakulov
BartMassey

bigocheatsheet.com

3/10

10/11/15

12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.

Big-O Algorithm Complexity Cheat Sheet

VinnieMagro
MiguelAmigot
DrewBailey
AneelNazareth
RahulChowdhury
RobertBurke
steven41292
BrandonAmos
MikeDavis
CasperVanGheluwe
JoelFriedly
Oleg
RenfredHarper
PiperChester
EricLefevreArdant
JonathanMcElroy
SiPham
mcverry
MaxHoffmann
AlejandroRamirez
DamonDavison
AlvinWan
AlanBriolat
DrewHannay
AndrewRasmussen
DennisTsang
BahadorSaket

Editthesetables!

Comments
SPONSORED

1.
agoanswers.comAnswersAnswers.com(sponsored)

11ThingsYou'llNeverBeAbleToUnseeamonth

2.
8MysteriousPhotosThatHaveYettobeExplaineda
weekagoanswers.comAnswersAnswers.com(sponsored)

bigocheatsheet.com

4/10

10/11/15

Big-O Algorithm Complexity Cheat Sheet

292Comments
3.

1 Lo
The10WorstActorsEverOnTVamonthago

BigOCheatSheet

Share

Recommend 107

SortbyBest

Jointhediscussion
MichaelMitchell 2yearsago

Thisisgreat.Maybeyoucouldincludesomeresources(linkstokhanacademy,moocetc)thatwouldexplain
eachoftheseconceptsforpeopletryingtolearnthem.
249

Reply Share

AmandaHarlin>MichaelMitchell 2yearsago

Yes!Please&thankyou
58

Reply Share

CamCecil>MichaelMitchell 2yearsago

Thisexplanationin'plainEnglish'helps:http://stackoverflow.com/quest...
23

Reply Share

ArjanNieuwenhuizen>MichaelMitchell 2yearsago

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.
11

Reply Share

fireheron>ArjanNieuwenhuizen 2yearsago

ThankyouArjan.Espaeciallythecoursera.orgone)
3

Reply Share

michaelfloering>fireheron 7monthsago

alsothis!http://opendatastructures.org
4

Reply Share

yth>michaelfloering 7monthsago

thankyouforsharingthis.
1

bigocheatsheet.com

Reply Share

5/10

10/11/15

Big-O Algorithm Complexity Cheat Sheet


Reply Share

BlakeJennings 2yearsago

i'mliterallycrying
77

Reply Share

JonRenner 2yearsago

Thisisgod'swork.
100

Reply Share

AdamHeinermann 2yearsago

Isthereaprinterfriendlyversion?
51

Reply Share

ThomasFeichtinger>AdamHeinermann ayearago

Actuallycopyingthecontentstoagoogledocworkedprettywell!
Ihavemadeitpublic,havealook:
https://docs.google.com/spread...
24

Reply Share

NumusSoftware>ThomasFeichtinger 14daysago

Youhavetolovealgorithms!!!


ericdrowell

Reply Share

Mod >AdamHeinermann

2yearsago

notyet,butthat'sagreatidea!
8

Reply Share

MattLabrum>AdamHeinermann 8monthsago

I,too,wantedaprinterfriendlyversionforstudyingbeforeaninterview,andIwasn'tsatisfiedwiththe
solutionsIfoundprovidedinthevariouscommentshere.So,IwentaheadandLaTeX'edthispageto
getanicePDF.
IhaveuploadedthePDFsIcreatedtomyGoogleDriveandmadethempublic:
https://drive.google.com/folde....InthatfolderaretwoPDFSoneisforlettersizedpaperandthe
otherisforA4sizedpaper.AssumingIdidn'tintroduceanytypos,thecontentofthosePDFsshould
matchthecontentofthispage(asitappearsatthismoment17February2015),withtheonly
noteworthydifferencebeingthatImovedtheGraphssectiontobeaftertheSortingsectiontohelp
eliminatesomeextrawhitespace.
2

Reply Share

YuvaraajSreenivasen>MattLabrum 4monthsago

GreatthanksMatt!!

Reply Share

JoeGibson>MattLabrum 8monthsago

Matt,
GreatjobontheLaTeXdocument.I'mpreparingforaGoogleinterviewandthiswillbealotof
help!
Anychanceyoucanputthe.texfileonyourdriveaswellinthesamefolder?

bigocheatsheet.com

Reply Share

6/10

10/11/15

Big-O Algorithm Complexity Cheat Sheet

MattLabrum>JoeGibson 8monthsago

Donethetwo.texfilesandthe.epsofthegrapharenowinthatfolder.
Edit
I'vealsoputtheRscriptandtmp.texfileIusedtocreatethegraphinthatfolder.After
creatingthe.epsfilewithR,IdidsomeprocessingonittogetthefinalBigO.epsfileI
includeinthe.texfiles.
Forcompleteness,togetfromtheRgeneratedBigO.epsfiletothefinalBigO.epsfile,I
didthefollowing:
1.OpenBigO.epswithatexteditortoensurethetextannotationshavenotbeen
brokenapart.Ipersonallyhadtoput"Operations"and"BigOComplexity"(yaxislabel
andgraphtitle)backtogether.
2.Processtmp.textogeta.dvifilethatcontainsaPSFrag'edversionofthegraph.
3.dvipsjEtmp.dvioBigO.tmp.eps
4.epstoolcopybboxBigO.tmp.epsBigO.eps
5.rmBigO.tmp.eps

Reply Share

JoeGibson>MattLabrum 8monthsago

Thanks,yourock.

Reply Share

GokceToykuyu 3yearsago

Couldweaddsometreealgorithmsandcomplexities?Thanks.IreallyliketheRedBlacktrees)
34

Reply Share

ericdrowell

Mod >GokceToykuyu

3yearsago

Excellentidea.I'lladdasectionthatcomparesinsertion,deletion,andsearchcomplexitiesforspecific
datastructures
30

Reply Share

DarrenLeRedgatr 2yearsago

Icameherefromanidletwitterclick.Ihavenoideawhatit'stalkingaboutoranyofthecomments.ButIlove
thefacttherearepeopleouttherethisclever.Makesmethinkthatonedayhumanitywillcomegood.Cheers.
55

Reply Share

ValentinStanciu 2yearsago

1.Deletion/insertioninasinglelinkedlistisimplementationdependent.Forthequestionof"Here'sapointerto
anelement,howmuchdoesittaketodeleteit?",singlelinkedliststakeO(N)sinceyouhavetosearchforthe
elementthatpointstotheelementbeingdeleted.Doublelinkedlistssolvethisproblem.
2.Hashescomeinamillionvarieties.HoweverwithagooddistributionfunctiontheyareO(logN)worstcase.
Usingadoublehashingalgorithm,youendupwithaworstcaseofO(loglogN).
3.Fortrees,thetableshouldprobablyalsocontainheapsandthecomplexitiesfortheoperation"Get
Minimum".
25

Reply Share

AlexisMas>ValentinStanciu ayearago

Ifyoualist:ABCD,WhenyouwanttodeleteB,youcandeleteanodewithoutiteratingoverthelist.
1.B.data=C.data
2.B.next=C.next
3.deleteC

bigocheatsheet.com

7/10

10/11/15

Big-O Algorithm Complexity Cheat Sheet

Ifyoucan'tcopydatabetweennodesbecauseitstooexpensivethenyes,it'sO(N)
5

Reply Share

Miguel>AlexisMas 10monthsago

Youstillhavetofindthepositioninthelist,whichcanonlybedonelinearly.
5

Reply Share

Guest>Miguel 8monthsago

Youstillhavetofindthepositioninthelist,whichcanonlybedonelinearly.
3

Reply Share

AlexisMas>Miguel 8monthsago

Yesofcourse,Ifyouneedtosearchthenodeit'sO(n),otherwiseyoucandeleteitasI
statedbefore.
1

Reply Share

Guest>AlexisMas 8monthsago

NoneedtofindthepositionifyoucandeleteitasAlexismentioned
2

Reply Share

OmegaNemesis28>AlexisMas 8monthsago

TogettoByouHAVEtoiterateoverthelistthough.Youcan'tjustmanipulateBwithouta
pointer.Sounlessyoudobookkeepingandhavepointerstospecificnodesyouintendto
delete/manipulate,LinkListsareO(n)insertanddelete.
2

Reply Share

AlexisMas>OmegaNemesis28 8monthsago

Strictlyspeakingno,youdon't.let'ssayyouhavethisfunction.
publicvoiddelete(Nodenode)
Thatfunctiondoesn'tcarehowdidyougotthatnode.
Didyougotmypoint?
Whenyouhaveapointertoanode,andthatnodeneedstobedeletedyoudon'tneedto
iteraveoverthelist.
1

Reply Share

OmegaNemesis28>AlexisMas 8monthsago

ButthatisMYpoint:p
YouhavetohavethenodeFIRST.Youhavetoiteratethroughthelistbeforeyoucando
that,unlessyoudobookkeepingandhappentohavesaidnode.RereadwhatIsaid.
"havepointerstospecificnodes"Mostofthetime,youdonotwithLinkedLists.Ifyou
haveaLinkedListandwanttodeleteindex5,youhavetoiterateto5andsuch.Your
examplewasABCD,ourpointsarethatyoutypicallydon'thavethepointertoBjust
offhand.YouhavetoobtainitfirstwhichwillbeO(n)
2

Reply Share

ChrisB>OmegaNemesis28 3daysago

Searchandinsert/deletearedifferentoperations.Insert/deleteonanunsortedlinkedlist
isO(1).Thefactthatyoumighthavetofirstsearchfortheelementthatyouwantto
deleteisnotconsideredrelevant,asthatfunctionalityiscoveredbytheO(n)search

bigocheatsheet.com

8/10

10/11/15

Big-O Algorithm Complexity Cheat Sheet

deleteisnotconsideredrelevant,asthatfunctionalityiscoveredbytheO(n)search
operation,nottheO(1)insert/deleteoperations.Arealworldexampleoflinkedlist
insert/deletecanbefoundinlist_delandlist_addoftheLinuxkernelsource,those
functionsareonly2and4linesofcode,soshouldbeeasytounderstand:http://lxr.free
electrons.com/...

Reply Share

SamLehman617>AlexisMas 7monthsago

Butinordertogettothatpointer,youprobablyneedtoiteratethroughthelist
1

Reply Share

PinguApp>AlexisMas 8monthsago

WhatifBisthelastelementinthelist?
HowwouldB'spredecesorknowthatitsnextfieldshouldpointtoNULLandnottoafuturely
invalidmemoryaddress?
1

Reply Share

AlexisMas>PinguApp 8monthsago

Inthatcaseyoucan'tdeletedthatway,you'reforcedtohaveapointertotheprevious
item.
1

Reply Share

qwertykeyboard 2yearsago

ItwouldbeveryhelpfultohaveexporttoPDF.Thx
17

Reply Share

Gene>qwertykeyboard 2yearsago

YoucouldconvertthedocumentyourselfusingPandoc:http://johnmacfarlane.net/pand...
Itmighttakeyoualongtimetogetitworking,butPandocisanamazingonestopshopforfile
conversion,andcrossplatformcompatible.
IfIunderstandbigohnotationcorrectlyImightsay"IestimateyourlearningrateforlearningPandoc
willbeO(1).".
2

Reply Share

Ashutosh>Gene 2yearsago

provedO(n),n=numberofformatconversionstolearn:)
4

Reply Share

JuanCarlosAlvarez>Gene 2yearsago

bigoh.hahafunny.
1

Reply Share

Guest 2yearsago

Finalsarealreadyover...Thisshouldhavebeensharedaweekago!Wouldhavesavedmelike45minutesof
usingWikipedia.
11

Reply Share

sigmaalgebra 2yearsago

Youomittedaninplace,guaranteedO(nlog(n))arraysort,
e.g.,heapsort.Youomittedradixsortthatcanbefaster
thananyofthealgorithmsyoumentioned.
MightmentionSATandrelatedproblemsinNPcomplete

bigocheatsheet.com

9/10

10/11/15

Big-O Algorithm Complexity Cheat Sheet

wherethebestknownalgorithmfor
aproblemofsizenhasO(2^n).
Mightincludeanactual,precisedefinitionofO().
9

Reply Share

JonRenner 2yearsago

AnywayIcangetaPDFversionwithouttakingscreenshotsmyself?
8

Reply Share

AttilaOlh>JonRenner 9monthsago

PrintDestination:ChangeSelect"SaveasPDF"(inChrome).
2

Reply Share

AntoineGrondin 2yearsago

IthinkDFSandBFS,underSearch,wouldbemoreappropriatelistedasGraphinsteadofTree.
8

Reply Share

ericdrowell

Mod >AntoineGrondin

2yearsago

Fixed!Thanks
4

Reply Share

QuentinPlepl>AntoineGrondin 2yearsago

Agreed
1

Reply Share

AnkushGupta 2yearsago

Awesomeresource!YoushouldaddDijkstrausingaFibonacciHeap!
7

Reply Share

tempire 2yearsago

Thischartseemsmisleading.BigOisworstcase,notaveragecase~isaveragecase.O(...)shouldn'tbe
usedintheaveragecasecolumns.
16

Reply Share

guest>tempire 2yearsago

IthinkbigOisjustanupperbound.Itcouldbeusedforall(best,worstandaverage)cases.AmI
wrong?

PagestylingviaBootstrap
CommentsviaDisqus
AlgorithmdetailviaWikipedia
TablesourcehostedonGithub
Mashupvia@ericdrowell

bigocheatsheet.com

10/10

You might also like