1. 程式人生 > >大O表示法演算法複雜度速查表(Big-O Algorithm Complexity Cheat Sheet)

大O表示法演算法複雜度速查表(Big-O Algorithm Complexity Cheat Sheet)

原文網址:http://bigocheatsheet.com/

Word文件下載:http://download.csdn.net/detail/anshan1984/5583399

 

 

Searching(搜尋演算法)

Algorithm(演算法)

Data Structure

(資料結構)

Time Complexity

(時間複雜度)

Space Complexity

(空間複雜度)

 

 

Average(平均)

Worst(最差)

Worst(最差)

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 search(二分查詢)

Sorted 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(Dijkstra最短路徑,使用最小堆作為優先佇列)

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
(Dijkstra最短路徑,使用無序陣列作為優先佇列)

Graph with |V| vertices and |E| edges

O(|V|^2)

O(|V|^2)

O(|V|)

Shortest path by Bellman-Ford(Bellman-Ford最短路徑法)

Graph with |V| vertices and |E| edges

O(|V||E|)

O(|V||E|)

O(|V|)

 

 

Sorting(排序演算法)

Algorithm(演算法)

Data Structure(資料結構)

Time Complexity(時間複雜度)

Worst Case Auxiliary Space Complexity

(最差額外消耗空間複雜度)

 

 

Best

Average

Worst

Worst

Quicksort

(快速排序)

Array(陣列)

O(n log(n))

O(n log(n))

O(n^2)

O(n)

Mergesort

(歸併排序)

Array

O(n log(n))

O(n log(n))

O(n log(n))

O(n)

Heapsort

(堆排序)

Array

O(n log(n))

O(n log(n))

O(n log(n))

O(1)

Bubble Sort

(氣泡排序)

Array

O(n)

O(n^2)

O(n^2)

O(1)

Insertion Sort

(插入排序)

Array

O(n)

O(n^2)

O(n^2)

O(1)

Select Sort

(選擇排序)

Array

O(n^2)

O(n^2)

O(n^2)

O(1)

Bucket Sort

(桶排序)

Array

O(n+k)

O(n+k)

O(n^2)

O(nk)

Radix Sort

(基數排序)

Array

O(nk)

O(nk)

O(nk)

O(n+k)

 

Heaps(堆)

Heaps

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(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 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|)

 

 

Data Structures(資料結構)

Data Structure

(資料結構)

Time Complexity

(時間複雜度)

Space Complexity

(空間複雜度)

 

Average(平均)

Worst(最差)

Worst(最差)

 

Indexing

Search

Insertion

Deletion

Indexing

Search

Insertion

Deletion

 

Basic Array

(基本陣列)

O(1)

O(n)

-

-

O(1)

O(n)

-

-

O(n)

Dynamic Array

(動態陣列)

O(1)

O(n)

O(n)

O(n)

O(1)

O(n)

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(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

(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)

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

(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)


 

 

 

Notation for asymptotic growth(漸進增長表示法)

Letter(字母)

Bound(限制)

Growth(增長)

(theta) Θ

upper and lower, tight[1]

equal[2]

(big-oh) O

upper, tightness unknown

less than or equal[3]

(small-oh) o

upper, not tight

less than

(big omega) Ω

lower, tightness unknown

greater than or equal

(small omega) ω

lower, not tight

greater than

[1] Big O is the upper bound,while Omega is the lower bound. Theta requires both Big O and Omega, so that'swhy it's referred to as atight bound (it must be boththe upper and lower bound). For example, an algorithm taking Omega(n log n)takes at least n log n time but has no upper limit. An algorithm taking Theta(nlog n) is far preferential since it takes AT LEAST n log n (Omega n log n) andNO MORE THAN n log n (Big O n log n).SO

大O是漸進上界,Ω是漸進下界。Θ需同時滿足大O和Ω,故稱為確界(必須同時符合上界和下界)。如,演算法Ω(nlogn)消耗至少nlogn時間,但是沒有上限。優先選擇演算法Θ(nlogn),因為它消耗至少nlogn(Ω(nlogn)),且不超過nlogn(O(nlogn))。

 

[2] f(x)=Θ(g(n)) means f (the running time of the algorithm) grows exactly like g when n (input size) gets larger. In other words, the growth rate of f(x) is asymptotically proportional to g(n).

f(x)=Θ(g(n))表示當n變大時,f(演算法執行時間)的增長與g嚴格相同。即,f(x)增長率漸進正比於g(n)。

 

[3] Same thing. Here the growth rate is no faster than g(n). big-oh is the most useful because represents the worst-case behavior.

同樣,這裡增長率不超過g(n)。O極其有用,因為它表示了最差效能。

 

In short, if algorithm is __ then its performance is __

algorithm

performance

o(n)

< n

O(n)

≤ n

Θ(n)

= n

Ω(n)

≥ n

ω(n)

> n

 

Big-O Complexity Chart