每个程序员都应该收藏的算法复杂度速查表
作者:
Eric
译者:
LCTT Xingyu.Wang
| 2016-06-20 12:36 评论: 7 收藏: 40
算法复杂度这件事
这篇文章覆盖了计算机科学里面常见算法的时间和空间的大 O 复杂度。我之前在参加面试前,经常需要花费很多时间从互联网上查找各种搜索和排序算法的优劣,以便我在面试时不会被问住。最近这几年,我面试了几家硅谷的初创企业和一些更大一些的公司,如 Yahoo、eBay、LinkedIn 和 Google,每次我都需要准备这个,我就在问自己,“为什么没有人创建一个漂亮的大 O 速查表呢?”所以,为了节省大家的时间,我就创建了这个,希望你喜欢!
--- Eric
图例
绝佳 | 不错 | 一般 | 不佳 | 糟糕 |
数据结构操作
数据结构 | 时间复杂度 | 空间复杂度 | |||||||
---|---|---|---|---|---|---|---|---|---|
平均 | 最差 | 最差 | |||||||
访问 | 搜索 | 插入 | 删除 | 访问 | 搜索 | 插入 | 删除 | ||
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) |
数组排序算法
算法 | 时间复杂度 | 空间复杂度 | ||
---|---|---|---|---|
最佳 | 平均 | 最差 | 最差 | |
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) |
图操作
节点 / 边界管理 | 存储 | 增加顶点 | 增加边界 | 移除顶点 | 移除边界 | 查询 |
---|---|---|---|---|---|---|
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|) |
堆操作
类型 | 时间复杂度 | ||||||
---|---|---|---|---|---|---|---|
Heapify | 查找最大值 | 分离最大值 | 提升键 | 插入 | 删除 | 合并 | |
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) |
大 O 复杂度图表
推荐阅读
- 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)
贡献者
- Eric Rowell, creator of Concrete.js, an HTML5 Canvas Framework
- Quentin Pleple
- Michael Abed
- Nick Dizazzo
- Adam Forsyth
- David Dorfman
- Jay Engineer
- Jennifer Hamon
- Josh Davis
- Nodir Turakulov
- Bart Massey
- Vinnie Magro
- Miguel Amigot
- Drew Bailey
- Aneel Nazareth
- Rahul Chowdhury
- Robert Burke
- steven41292
- Brandon Amos
- Mike Davis
- Casper Van Gheluwe
- Joel Friedly
- Oleg
- Renfred Harper
- Piper Chester
- Eric Lefevre-Ardant
- Jonathan McElroy
- Si Pham
- mcverry
- Max Hoffmann
- Alejandro Ramirez
- Damon Davison
- Alvin Wan
- Alan Briolat
- Drew Hannay
- Andrew Rasmussen
- Dennis Tsang
- Bahador Saket
- cicada [Chrome 54.0|Windows 7] 2016-10-30 20:54 11 赞 回复
- 我要收藏了
- hello_linux [Firefox 49.0|Windows 7] 2016-07-08 14:07 11 赞 回复
- 资料不错。
- Loki-A [Firefox 47.0|Windows 10] 2016-06-22 14:52 11 赞 回复
- 已收藏,方便日后查阅
- AlexMahoneFBI [Maxthon 4.9|Windows 7] 2016-06-22 08:03 10 赞 回复
- 很不错啊,总结的,点赞
- ryt [Chrome 51.0|GNU/Linux] 2016-06-21 08:37 16 赞 回复
- 总结的很好。
- fxbszj [Firefox 47.0|Windows 7] 2016-06-20 15:37 12 赞 回复
- 看看
LCTT 译者
Xingyu.Wang 💎💎💎
共计翻译: 995.0 篇
| 共计贡献: 3546 天
贡献时间:2014-07-25 -> 2024-04-09
访问我的 LCTT 主页 | 在 GitHub 上关注我