Linux中国

每个程序员都应该收藏的算法复杂度速查表

图例

绝佳 不错 一般 不佳 糟糕

数据结构操作

数据结构 时间复杂度 空间复杂度
平均 最差 最差
访问 搜索 插入 删除 访问 搜索 插入 删除
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](http://en.wikipedia.org/wiki/Bucket_sort "Only for integers. k is a number of buckets") O(n+k) O(n+k) O(n^2) O(n)
[Radix Sort](http://en.wikipedia.org/wiki/Radix_sort "Constant number of digits 'k'") 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 复杂度图表

![Big O Complexity Graph](/data/attachment/album/201606/20/123634szm02anm9jm6qqbs.png "Big O Complexity Graph")

推荐阅读

贡献者

  1. Eric Rowell, creator of Concrete.js, an HTML5 Canvas Framework
  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
  31. Alejandro Ramirez
  32. Damon Davison
  33. Alvin Wan
  34. Alan Briolat
  35. Drew Hannay
  36. Andrew Rasmussen
  37. Dennis Tsang
  38. Bahador Saket

本文转载来自 Linux 中国: https://github.com/Linux-CN/archive

对这篇文章感觉如何?

太棒了
0
不错
0
爱死了
0
不太好
0
感觉很糟
0
雨落清风。心向阳

    You may also like

    Leave a reply

    您的电子邮箱地址不会被公开。 必填项已用 * 标注

    此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

    More in:Linux中国