Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Time Complexity

The document is a cheat sheet that provides information on the time and space complexities of common algorithms and data structures. It includes tables summarizing the complexities of operations on data structures like arrays, stacks, linked lists, trees, and graphs. It also includes tables summarizing the complexities of sorting algorithms and heap operations. The cheat sheet was created by Eric Rowell to help himself and others prepare for technical interviews.

Uploaded by

nrd522tjcc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Time Complexity

The document is a cheat sheet that provides information on the time and space complexities of common algorithms and data structures. It includes tables summarizing the complexities of operations on data structures like arrays, stacks, linked lists, trees, and graphs. It also includes tables summarizing the complexities of sorting algorithms and heap operations. The cheat sheet was created by Eric Rowell to help himself and others prepare for technical interviews.

Uploaded by

nrd522tjcc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Big-O Algorithm Complexity Cheat Sheet http://bigocheatsheet.

com/

Big-O Cheat Sheet

Data Structures
Sorting
Graphs
Heaps
Chart
Comments

I receive
$4.69
on Gratipay.

4,165 2.5k

Tweet

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 Yahoo, eBay, LinkedIn, and Google, 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

Legend
Excellent Good Fair Bad Horrible

1 of 6 05/11/2015 11:53 AM
Big-O Algorithm Complexity Cheat Sheet http://bigocheatsheet.com/

Data Structure Operations


Data Structure Time Complexity Space 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)
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 - O(1) O(1) O(1) - 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 - O(log(n)) O(log(n)) O(log(n)) - 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 - O(log(n)) O(log(n)) O(log(n)) - 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)

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)

Shell Sort O(n) O((nlog(n))^2) O((nlog(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)

Graph Operations
Node / Edge Management 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 Operations
Type Time Complexity
Heapify Find Max Extract Max Increase Key Insert Delete Merge
Linked List (sorted) - O(1) O(1) O(n) O(n) O(1) O(m+n)
Linked List (unsorted) - O(n) O(n) O(1) O(1) O(1) O(1)

Binary Heap O(n) O(1) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(m+n)


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)

Big-O Complexity Chart

2 of 6 05/11/2015 11:53 AM
Big-O Algorithm Complexity Cheat Sheet http://bigocheatsheet.com/

Recommended Reading
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)

Contributors
Edit these tables!

1. Eric Rowell
2. Quentin Pleple
3. Michael Abed
4. Nick Dizazzo
5. Adam Forsyth
6. David Dorfman
7. Jay Engineer
8. Jennifer Hamon
9. Josh Davis
10. Nodir Turakulov
11. Bart Massey
12. Vinnie Magro
13. Miguel Amigot
14. Drew Bailey
15. Aneel Nazareth
16. Rahul Chowdhury
17. Robert Burke
18. steven41292
19. Brandon Amos
20. Mike Davis
21. Casper Van Gheluwe
22. Joel Friedly
23. Oleg
24. Renfred Harper
25. Piper Chester
26. Eric Lefevre-Ardant
27. Jonathan McElroy
28. Si Pham
29. mcverry
30. Max Hoffmann

3 of 6 05/11/2015 11:53 AM
Big-O Algorithm Complexity Cheat Sheet http://bigocheatsheet.com/

31. Alejandro Ramirez


32. Damon Davison
33. Alvin Wan
34. Alan Briolat
35. Drew Hannay
36. Andrew Rasmussen
37. Dennis Tsang
38. Bahador Saket

4 of 6 05/11/2015 11:53 AM
Big-O Algorithm Complexity Cheat Sheet http://bigocheatsheet.com/

267 Comments Big-O Cheat Sheet  Login

 Recommend 93 ⤤ Share Sort by Best

Join the discussion…

Michael Mitchell • 2 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.
242 • Reply • Share ›

Amanda Harlin > Michael Mitchell • 2 years ago


Yes! Please & thank you
56 • Reply • Share ›

Cam Cecil > Michael Mitchell • 2 years ago


This explanation in 'plain English' helps: http://stackoverflow.com/quest...
22 • Reply • Share ›

Arjan Nieuwenhuizen > Michael Mitchell • 2 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.
11 • Reply • Share ›

fireheron > Arjan Nieuwenhuizen • 2 years ago


Thank you Arjan. Espaecially the coursera.org one ;-)
3 • Reply • Share ›

michaelfloering > fireheron • 2 months ago


also this! http://opendatastructures.org
4 • Reply • Share ›

yth > michaelfloering • 2 months ago


thank you for sharing this.
1 • Reply • Share ›

Blake Jennings • 2 years ago


i'm literally crying
76 • Reply • Share ›

Jon Renner • 2 years ago


This is god's work.
100 • Reply • Share ›

5 of 6 05/11/2015 11:53 AM
Big-O Algorithm Complexity Cheat Sheet http://bigocheatsheet.com/

Page styling via Bootstrap


Comments via Disqus
Algorithm detail via Wikipedia
Table source hosted on Github
Mashup via @ericdrowell

6 of 6 05/11/2015 11:53 AM

You might also like