0% found this document useful (0 votes)
449 views7 pages

Big-O Algorithm Complexity Cheat Sheet

This document provides a "Big-O Algorithm Complexity Cheat Sheet" with time and space complexities for common algorithms and data structures. It includes charts summarizing the average, best, and worst case time complexities of operations like access, search, insertion, and deletion for data structures like arrays, linked lists, trees, and graphs. It also summarizes time complexities for common sorting algorithms like quicksort, mergesort, heapsort, and more. The cheat sheet is intended to help review these concepts for technical interviews.

Uploaded by

BsmPrasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
449 views7 pages

Big-O Algorithm Complexity Cheat Sheet

This document provides a "Big-O Algorithm Complexity Cheat Sheet" with time and space complexities for common algorithms and data structures. It includes charts summarizing the average, best, and worst case time complexities of operations like access, search, insertion, and deletion for data structures like arrays, linked lists, trees, and graphs. It also summarizes time complexities for common sorting algorithms like quicksort, mergesort, heapsort, and more. The cheat sheet is intended to help review these concepts for technical interviews.

Uploaded by

BsmPrasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

8/5/2016 Big­O 

Algorithm Complexity Cheat Sheet

Big­O Cheat Sheet Download PDF

17k
SHARES  Share 13.6k Tweet Share 2.8k Share 517 Submit 0

Know Thy Complexities!
Hi there!  This webpage covers the space and time Big­O 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 Google, Facebook, Yahoo, LinkedIn, and eBay, and each time that I prepared for an interview, I
thought to myself "Why hasn't someone created a nice Big­O cheat sheet?".  So, to save all of you fine folks a ton of time, I went ahead and created
one.  Enjoy! ­ Eric

If you're trying to catch them all, you might also check out the Pokemon Go Evolution Chart.

Big­O Complexity Chart
Excellent Good Fair Bad Horrible

O(n!) O(2^n)
O(n^2)

O(n log n)
Operations

O(n)

O(1), O(log n)

Elements

Common Data Structure Operations
Space
Data Structure Time Complexity
Complexity
Average Worst Worst
Access Search Insertion Deletion Access Search Insertion Deletion
Array O(1) O(n) O(n) O(n) O(1) O(n) O(n) O(n) O(n)

Stack O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)

Queue O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)

Singly­Linked List O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)

Doubly­Linked List O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)

Skip List O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n) O(n) O(n) O(n) O(n log(n))

Hash Table N/A O(1) O(1) O(1) N/A O(n) O(n) O(n) O(n)

Binary Search Tree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n) O(n) O(n) O(n) O(n)

Cartesian Tree N/A O(log(n)) O(log(n)) O(log(n)) N/A O(n) O(n) O(n) O(n)

B­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)

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 N/A O(log(n)) O(log(n)) O(log(n)) N/A O(log(n)) O(log(n)) O(log(n)) O(n)

AVL 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)
http://www.bigocheatsheet.com/ 1/7
8/5/2016 Big­O Algorithm Complexity Cheat Sheet
AVL 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)

KD Tree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n) O(n) O(n) O(n) O(n)

Array Sorting Algorithms
Algorithm Time Complexity Space Complexity
Best Average Worst Worst
Quicksort O(n log(n)) O(n log(n)) O(n^2) O(log(n))

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

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

Heapsort O(n log(n)) O(n log(n)) O(n log(n)) O(1)

Bubble Sort O(n) O(n^2) O(n^2) O(1)

Insertion Sort O(n) O(n^2) O(n^2) O(1)

Selection Sort O(n^2) O(n^2) O(n^2) O(1)

Tree Sort O(n log(n)) O(n log(n)) O(n^2) O(n)

Shell Sort O(n log(n)) O(n(log(n))^2) O(n(log(n))^2) O(1)

Bucket Sort O(n+k) O(n+k) O(n^2) O(n)

Radix Sort O(nk) O(nk) O(nk) O(n+k)

Counting Sort O(n+k) O(n+k) O(n+k) O(k)

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

Graph Data Structure Operations
Data Structure Time Complexity
Storage Add Vertex Add Edge Remove Vertex Remove Edge Query
Adjacency list O(|V|+|E|) O(1) O(1) O(|V| + |E|) O(|E|) O(|V|)

Incidence list O(|V|+|E|) O(1) O(1) O(|E|) O(|E|) O(|E|)

Adjacency matrix O(|V|^2) O(|V|^2) O(1) O(|V|^2) O(1) O(1)

Incidence matrix O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|E|)

Heap Data Structure Operations
Data Structure Time Complexity
Find Max Extract Max Increase Key Insert Delete Merge
Binary Heap O(1) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(m+n)

Pairing Heap O(1) O(log(n)) O(log(n)) O(1) O(log(n)) O(1)

Binomial Heap O(1) O(log(n)) O(log(n)) O(1) O(log(n)) O(log(n))

Fibonacci Heap O(1) O(log(n)) O(1) O(1) O(log(n)) O(1)

Graph Algorithms
Algorithm Time Complexity Space Complexity
Average Worst Worst
Dijkstra's algorithm O(|E| log |V|) O(|V|^2) O(|V| + |E|)

A* search algorithm O(|E|) O(b^d) O(b^d)

Prim's algorithm O(|E| log |V|) O(|V|^2) O(|V| + |E|)

Bellman–Ford algorithm O(|E| ⋅ |V|) O(|E| ⋅ |V|) O(|V|)

Floyd­Warshall algorithm O(|V|^3) O(|V|^3) O(|V|^2)

Topological sort O(|V| + |E|) O(|V| + |E|) O(|V| + |E|)

Learn More
Cracking the Coding Interview: 150 Programming Questions and Solutions
Introduction to Algorithms, 3rd Edition
Data Structures and Algorithms in Java (2nd Edition)
High Performance JavaScript (Build Faster Web Application Interfaces)
http://www.bigocheatsheet.com/ Contributors 2/7
8/5/2016 Big­O Algorithm Complexity Cheat Sheet

Contributors
Eric Rowell   Quentin Pleple   Michael Abed   Nick Dizazzo   Adam Forsyth   Felix Zhu   Jay Engineer   Josh Davis   Nodir Turakulov
Jennifer Hamon   David Dorfman   Bart Massey   Ray Pereda   Si Pham   Mike Davis   mcverry   Max Hoffmann   Bahador Saket
Damon Davison   Alvin Wan   Alan Briolat   Drew Hannay   Andrew Rasmussen   Dennis Tsang   Vinnie Magro   Adam Arold   Alejandro Ramirez
Aneel Nazareth   Rahul Chowdhury   Jonathan McElroy   steven41292   Brandon Amos   Joel Friedly   Casper Van Gheluwe
Eric Lefevre­Ardant   Oleg   Renfred Harper   Piper Chester   Miguel Amigot   Apurva K   Matthew Daronco   Yun­Cheng Lin   Clay Tyler
Orhan Can Ozalp   Ayman Singh   David Morton   Aurelien Ooms   Sebastian Paaske Torholm   Koushik Krishnan   Drew Bailey   Robert Burke

Make this Page Better
Edit these tables!

326 Comments Big­O Cheat Sheet 
1  Login

  Recommend  155 ⤤ Share Sort by Best

Join the discussion…

Michael Mitchell • 3 years ago − ⚑
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.
267 △   ▽ • Reply • Share ›

Amanda Harlin > Michael Mitchell • 3 years ago − ⚑
Yes! Please & thank you
60 △   ▽ • Reply • Share ›

Cam Cecil > Michael Mitchell • 3 years ago − ⚑
This explanation in 'plain English' helps: http://stackoverflow.com/quest...
28 △   ▽ • Reply • Share ›

Richard Wheatley > Cam Cecil • 9 months ago − ⚑
this is plain english.
△   ▽ • Reply • Share ›

Arjan Nieuwenhuizen > Michael Mitchell • 3 years ago − ⚑
Here are the links that I know of.

#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.
13 △   ▽ • Reply • Share ›

fireheron > Arjan Nieuwenhuizen • 3 years ago − ⚑
Thank you Arjan. Espaecially the coursera.org one ;­)
3 △   ▽ • Reply • Share ›

@hangtwentyy > fireheron • a year ago − ⚑
also this! http://opendatastructures.org
5 △   ▽ • Reply • Share ›

yth > @hangtwentyy • a year ago − ⚑
thank you for sharing this.
http://www.bigocheatsheet.com/ 3/7
8/5/2016 Big­O Algorithm Complexity Cheat Sheet
thank you for sharing this.
1 △   ▽ • Reply • Share ›

Eduardo Sánchez > Michael Mitchell • 7 months ago − ⚑
There is an amazing tutorial for Big O form Derek Banas in Youtube, that guy is amazing explaining!!!


see more

2 △   ▽ • Reply • Share ›

mishrsud > Eduardo Sánchez • 9 days ago − ⚑
Cool! This is a more than adequate introduction! Thanks a ton for sharing!
△   ▽ • Reply • Share ›

Blake Jennings • 3 years ago − ⚑
i'm literally crying
83 △   ▽ • Reply • Share ›

Jon Renner • 3 years ago − ⚑
This is god's work.
110 △   ▽ • Reply • Share ›

Adam Heinermann • 3 years ago − ⚑
Is there a printer­friendly version?
52 △   ▽ • Reply • Share ›

Thomas Feichtinger > Adam Heinermann • 2 years ago − ⚑
Actually copying the contents to a google doc worked pretty well!

I have made it public, have a look:
https://docs.google.com/spread...
26 △   ▽ • Reply • Share ›

mishrsud > Thomas Feichtinger • 9 days ago − ⚑
Thank you! I sometimes wish entire humanity was as benevolent as us programmers when it comes to sharing the fruits of our labour! The
world would be so much better :­)
1 △   ▽ • Reply • Share ›

David Joo > Thomas Feichtinger • 11 days ago − ⚑
Thank you for doing that!
△   ▽ • Reply • Share ›

Numus Software > Thomas Feichtinger • 10 months ago − ⚑
You have to love algorithms !!!
△   ▽ • Reply • Share ›

ericdrowell  Mod   > Adam Heinermann  •  3 years ago − ⚑


not yet, but that's a great idea!
8 △   ▽ • Reply • Share ›

Matt Labrum > Adam Heinermann • a year ago − ⚑
I, too, wanted a printer­friendly version for studying before an interview, and I wasn't satisfied with the solutions I found provided in the various
comments here. So, I went ahead and LaTeX'ed this page to get a nice PDF.
http://www.bigocheatsheet.com/ 4/7
8/5/2016 Big­O Algorithm Complexity Cheat Sheet
comments here. So, I went ahead and LaTeX'ed this page to get a nice PDF.

I have uploaded the PDFs I created to my Google Drive and made them public: https://drive.google.com/folde... . In that folder are two PDFS ­­­ one
is for letter­sized paper and the other is for A4­sized paper. Assuming I didn't introduce any typos, the content of those PDFs should match the
content of this page (as it appears at this moment; 17 February 2015), with the only noteworthy difference being that I moved the Graphs section to
be after the Sorting section to help eliminate some extra white space.
5 △   ▽ • Reply • Share ›

Yuvaraaj Sreenivasen > Matt Labrum • a year ago − ⚑
Great thanks Matt!!
△   ▽ • Reply • Share ›

Joe Gibson > Matt Labrum • a year ago − ⚑
Matt,

Great job on the LaTeX document. I'm preparing for a Google interview and this will be a lot of help!

Any chance you can put the .tex file on your drive as well in the same folder?
△   ▽ • Reply • Share ›

Matt Labrum > Joe Gibson • a year ago − ⚑
Done; the two .tex files and the .eps of the graph are now in that folder.

­­­Edit­­­
I've also put the R script and tmp.tex file I used to create the graph in that folder. After creating the .eps file with R, I did some
processing on it to get the final Big­O.eps file I include in the .tex files.

For completeness, to get from the R­generated Big­O.eps file to the final Big­O.eps file, I did the following:
1. Open Big­O.eps with a text editor to ensure the text annotations have not been broken apart. I personally had to put "Operations"
and "Big­O Complexity" (y­axis label and graph title) back together. 
2. Process tmp.tex to get a .dvi file that contains a PSFrag'ed version of the graph.
3. dvips ­j ­E tmp.dvi ­o Big­O.tmp.eps
4. epstool ­­copy ­­bbox Big­O.tmp.eps Big­O.eps
5. rm Big­O.tmp.eps
△   ▽ • Reply • Share ›

Joe Gibson > Matt Labrum • a year ago − ⚑
Thanks, you rock.
△   ▽ • Reply • Share ›

Gokce Toykuyu • 4 years ago − ⚑
Could we add some tree algorithms and complexities? Thanks. I really like the Red­Black trees ;)
34 △   ▽ • Reply • Share ›

ericdrowell  Mod   > Gokce Toykuyu  •  4 years ago − ⚑


Excellent idea. I'll add a section that compares insertion, deletion, and search complexities for specific data structures
30 △   ▽ • Reply • Share ›

Valentin Stanciu • 3 years 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?", single­linked lists take O(N) since you have to search for the element that points to the element being deleted. Double­linked 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".
25 △   ▽ • Reply • Share ›

Alexis Mas > Valentin Stanciu • 2 years ago − ⚑
If you a list: A B C D, When you want to delete B, you can delete a node without iterating over the list.

1. B.data = C.data
2. B.next = C.next
3. delete C

If you can't copy data between nodes because its too expensive then yes, it's O(N)
6 △   ▽ • Reply • Share ›

Miguel > Alexis Mas • 2 years ago − ⚑
You still have to find the position in the list, which can only be done linearly.
6 △   ▽ • Reply • Share ›

http://www.bigocheatsheet.com/ − 5/7
8/5/2016 Big­O Algorithm Complexity Cheat Sheet
Guest > Miguel • a year ago − ⚑
You still have to find the position in the list, which can only be done linearly.
3 △   ▽ • Reply • Share ›

Alexis Mas > Miguel • a year ago − ⚑
Yes of course, If you need to search the node it's O(n), otherwise you can delete it as I stated before.
1 △   ▽ • Reply • Share ›

Guest > Alexis Mas • a year ago − ⚑
No need to find the position if you can delete it as Alexis mentioned
2 △   ▽ • Reply • Share ›

OmegaNemesis28 > Alexis Mas • a year ago − ⚑
To get to B ­ you HAVE to iterate over the list though. You can't just manipulate B without a pointer. So unless you do book­keeping and have
pointers to specific nodes you intend to delete/manipulate, LinkLists are O(n) insert and delete.
2 △   ▽ • Reply • Share ›

Alexis Mas > OmegaNemesis28 • a year ago − ⚑
Strictly speaking no, you don't. let's say you have this function.

public void delete(Node node)

That function doesn't care how did you got that node.

Did you got my point?

When you have a pointer to a node, and that node needs to be deleted you don't need to iterave over the list.
1 △   ▽ • Reply • Share ›

Sam Lehman > Alexis Mas • a year ago − ⚑
But in order to get to that pointer, you probably need to iterate through the list
2 △   ▽ • Reply • Share ›

OmegaNemesis28 > Alexis Mas • a year ago − ⚑
But that is MY point :p

You have to have the node FIRST. You have to iterate through the list before you can do that, unless you do book­keeping and
happen to have said node. Reread what I said. "have pointers to specific nodes" Most of the time, you do not with LinkedLists. If you
have a Linked List and want to delete index 5, you have to iterate to 5 and such. Your example was ABCD, our points are that you
typically don't have the pointer to B just offhand. You have to obtain it first which will be O(n)
2 △   ▽ • Reply • Share ›

Chris B > OmegaNemesis28 • 10 months ago − ⚑
Search and insert/delete are different operations. Insert/delete on an unsorted linked list is O(1). The fact that you might have to first
search for the element that you want to delete is not considered relevant, as that functionality is covered by the O(n) search
operation, not the O(1) insert/delete operations. A real world example of linked list insert/delete can be found in list_del and list_add of
the Linux kernel source, those functions are only 2 and 4 lines of code, so should be easy to understand: http://lxr.free­
electrons.com/...
1 △   ▽ • Reply • Share ›

Pingu App > Alexis Mas • a year ago − ⚑
What if B is the last element in the list?
How would B's predecesor know that its next field should point to NULL and not to a futurely invalid memory address?
1 △   ▽ • Reply • Share ›

Alexis Mas > Pingu App • a year ago − ⚑
In that case you can't deleted that way, you're forced to have a pointer to the previous item.
1 △   ▽ • Reply • Share ›

Darren Le Redgatr • 3 years 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 people out there this clever.
Makes me think that one day humanity will come good. Cheers.
57 △   ▽ • Reply • Share ›

qwertykeyboard • 3 years ago − ⚑
It would be very helpful to have export to PDF. Thx
17 △   ▽ • Reply • Share ›

Gene > qwertykeyboard • 3 years ago − ⚑
http://www.bigocheatsheet.com/ 6/7
8/5/2016 Big­O Algorithm Complexity Cheat Sheet
Gene > qwertykeyboard • 3 years ago − ⚑
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 years ago − ⚑
proved O(n), n=number of format conversions to learn :)
4 △   ▽ • Reply • Share ›

Juan Carlos Alvarez > Gene • 2 years ago − ⚑
big oh. haha funny.
1 △   ▽ • Reply • Share ›

Guest • 3 years ago − ⚑
Finals are already over... This should have been shared a week ago! Would have saved me like 45 minutes of using Wikipedia.
11 △   ▽ • Reply • Share ›

Jon Renner • 3 years ago − ⚑
Anyway I can get a PDF version without taking screenshots myself?
9 △   ▽ • Reply • Share ›

Attila Oláh > Jon Renner • 2 years ago − ⚑
Print → Destination: Change → Select "Save as PDF" (in Chrome).
3 △   ▽ • Reply • Share ›

sigmaalgebra • 3 years ago − ⚑
You omitted an in­place, 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 NP­complete

where the best known algorithm for
a problem of size n has O(2^n).

Might include an actual, precise definition of O().
9 △   ▽ • Reply • Share ›

Ankush Gupta • 3 years ago − ⚑
Awesome resource! You should add Dijkstra using a Fibonacci Heap!
8 △   ▽ • Reply • Share ›

Antoine Grondin • 3 years ago − ⚑
I think DFS and BFS, under Search, would be more appropriate listed as Graph instead of Tree.
8 △   ▽ • Reply • Share ›

ericdrowell  Mod   > Antoine Grondin  •  3 years ago − ⚑


Fixed! Thanks
4 △   ▽ • Reply • Share ›

http://www.bigocheatsheet.com/ 7/7

You might also like