您的位置:首页 > 理论基础 > 数据结构算法

时间复杂度和空间复杂度——总结

2013-05-05 22:25 363 查看



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 msyelf "Why oh 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!
Good
Fair
Poor


Searching

AlgorithmData StructureTime ComplexitySpace Complexity
  AverageWorstWorst
Depth First Search (DFS)Graph of |V| vertices and |E| edges
-
O(|E| + |V|)
O(|V|)
Breadth First Search (BFS)Graph of |V| vertices and |E| edges
-
O(|E| + |V|)
O(|V|)
Binary searchSorted array of n elements
O(log(n))
O(log(n))
O(1)
Linear (Brute Force)Array
O(n)
O(n)
O(1)
Shortest path by Dijkstra,

using a Min-heap as priority queue
Graph with |V| vertices and |E| edges
O((|V| + |E|) log |V|)
O((|V| + |E|) log |V|)
O(|V|)
Shortest path by Dijkstra,

using an unsorted array as priority queue
Graph with |V| vertices and |E| edges
O(|V|^2)
O(|V|^2)
O(|V|)
Shortest path by Bellman-FordGraph with |V| vertices and |E| edges
O(|V||E|)
O(|V||E|)
O(|V|)


Sorting

AlgorithmData StructureTime ComplexityWorst Case Auxiliary Space Complexity
  BestAverageWorstWorst
QuicksortArray
O(n log(n))
O(n log(n))
O(n^2)
O(log(n))
MergesortArray
O(n log(n))
O(n log(n))
O(n log(n))
O(n)
HeapsortArray
O(n log(n))
O(n log(n))
O(n log(n))
O(1)
Bubble SortArray
O(n)
O(n^2)
O(n^2)
O(1)
Insertion SortArray
O(n)
O(n^2)
O(n^2)
O(1)
Select SortArray
O(n^2)
O(n^2)
O(n^2)
O(1)
Bucket SortArray
O(n+k)
O(n+k)
O(n^2)
O(nk)
Radix SortArray
O(nk)
O(nk)
O(nk)
O(n+k)


Data Structures

Data StructureTime ComplexitySpace Complexity
 AverageWorstWorst
 IndexingSearchInsertionDeletionIndexingSearchInsertionDeletion 
Basic Array
O(1)
O(n)
-
-
O(1)
O(n)
-
-
O(n)
Dynamic Array
O(1)
O(n)
O(n)
-
O(1)
O(n)
O(n)
-
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(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(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(n)
Red-Black 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(n)


Heaps

HeapsTime Complexity
 HeapifyFind MaxExtract MaxIncrease KeyInsertDeleteMerge 
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(log(n))
O(1)
O(log(n))
O(log(n))
O(log(n))
O(log(n))
O(m+n)
Binomial Heap
-
O(log(n))
O(log(n))
O(log(n))
O(log(n))
O(log(n))
O(log(n))
Fibonacci Heap
-
O(1)
O(log(n))*
O(1)*
O(1)
O(log(n))*
O(1)


Graphs

Node / Edge ManagementStorageAdd VertexAdd EdgeRemove VertexRemove EdgeQuery
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|)


Big-O Complexity Graph

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 算法