常见的算法和数据结构的时间复杂度和空间复杂度

Posted 石工记

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常见的算法和数据结构的时间复杂度和空间复杂度相关的知识,希望对你有一定的参考价值。

英文版链接:Big-O Algorithm Complexity Cheat Sheet (Know Thy Complexities!) @ericdrowell

搜索

算法数据结构时间复杂度空间复杂度
平均最差最差
深度优先搜索 (DFS)Graph of |V| vertices and |E| edges-O(|E| + |V|)O(|V|)
广度优先搜索 (BFS)Graph of |V| vertices and |E| edges-O(|E| + |V|)O(|V|)
二分查找Sorted array of n elementsO(log(n))O(log(n))O(1)
穷举查找ArrayO(n)O(n)O(1)
最短路径-Dijkstra,用小根堆作为优先队列Graph with |V| vertices and |E| edgesO((|V| + |E|) log |V|)O((|V| + |E|) log |V|)O(|V|)
最短路径-Dijkstra,用无序数组作为优先队列Graph with |V| vertices and |E| edgesO(|V|^2)O(|V|^2)O(|V|)
最短路径-Bellman-FordGraph with |V| vertices and |E| edgesO(|V||E|)O(|V||E|)O(|V|)

排序

算法数据结构时间复杂度最坏情况下的辅助空间复杂度
最佳平均最差最差
快速排序数组O(n log(n))O(n log(n))O(n^2)O(n)
归并排序数组O(n log(n))O(n log(n))O(n log(n))O(n)
堆排序数组O(n log(n))O(n log(n))O(n log(n))O(1)
冒泡排序数组O(n)O(n^2)O(n^2)O(1)
插入排序数组O(n)O(n^2)O(n^2)O(1)
选择排序数组O(n^2)O(n^2)O(n^2)O(1)
桶排序数组O(n+k)O(n+k)O(n^2)O(nk)
基数排序数组O(nk)O(nk)O(nk)O(n+k)

数据结构

数据结构时间复杂度空间复杂度
平均最差最差
索引查找插入删除索引查找插入删除
基本数组O(1)O(n)--O(1)O(n)--O(n)
动态数组O(1)O(n)O(n)O(n)O(1)O(n)O(n)O(n)O(n)
单链表O(n)O(n)O(1)O(1)O(n)O(n)O(1)O(1)O(n)
双链表O(n)O(n)O(1)O(1)O(n)O(n)O(1)O(1)O(n)
跳表O(log(n))O(log(n))O(log(n))O(log(n))O(n)O(n)O(n)O(n)O(n log(n))
哈希表-O(1)O(1)O(1)-O(n)O(n)O(n)O(n)
二叉搜索树O(log(n))O(log(n))O(log(n))O(log(n))O(n)O(n)O(n)O(n)O(n)
笛卡尔树-O(log(n))O(log(n))O(log(n))-O(n)O(n)O(n)O(n)
B-树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)
红黑树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)
伸展树-O(log(n))O(log(n))O(log(n))-O(log(n))O(log(n))O(log(n))O(n)
AVL 树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)

Heaps时间复杂度
建堆查找最大值提取最大值Increase Key插入删除合并
链表(已排序)-O(1)O(1)O(n)O(n)O(1)O(m+n)
链表(未排序)-O(n)O(n)O(1)O(1)O(1)O(1)
二叉堆O(n)O(1)O(log(n))O(log(n))O(log(n))O(log(n))O(m+n)
二项堆-O(log(n))O(log(n))O(log(n))O(log(n))O(log(n))O(log(n))
斐波那契堆-O(1)O(log(n))*O(1)*O(1)O(log(n))*O(1)

节点 / 边 管理StorageAdd VertexAdd EdgeRemove VertexRemove EdgeQuery
邻接表O(|V|+|E|)O(1)O(1)O(|V| + |E|)O(|E|)O(|V|)
关联表O(|V|+|E|)O(1)O(1)O(|E|)O(|E|)O(|E|)
邻接矩阵O(|V|^2)O(|V|^2)O(1)O(|V|^2)O(1)O(1)
关联矩阵O(|V| ⋅ |E|)O(|V| ⋅ |E|)O(|V| ⋅ |E|)O(|V| ⋅ |E|)O(|V| ⋅ |E|)O(|E|)

以上是关于常见的算法和数据结构的时间复杂度和空间复杂度的主要内容,如果未能解决你的问题,请参考以下文章

开卷数据结构?时间和空间复杂度你可得把握住!!不行就让叔来~

01时间复杂度和空间复杂度

[ 数据结构-C语言 ] 算法的时间复杂度

常见排序算法及对应的时间复杂度和空间复杂度

常见排序算法及对应的时间复杂度和空间复杂度

常见排序算法及对应的时间复杂度和空间复杂度