Time Complexity (时间复杂度)

来自cppreference.com
< cpp


There are different measurements of the speed of any given algorithm. Given an input size of N, they can be described as follows:

不同的算法,速度也会不一样。对于一个输入大小 N,它们可以如下描述:

NameSpeedDescriptionFormulaExample
factorial timeslowertakes an amount of time proportional to N raised to the Nth powerN!Brute force solution to Traveling Salesman Problem
exponential timeslowtakes an amount of time proportional to a constant raised to the Nth powerKNBrute force solution to Rubik's Cube
polynomial timefasttakes an amount of time proportional to N raised to some constant powerNKComparison sorts (bubble, insertion, selection sort)
linearithmic timefastertakes an amount of time between linear and polynomialN * log(N)The Linear logarithmic sorts (quicksort, heapsort, mergesort)
linear timeeven fastertakes an amount of time directly proportional to NK * NIterating through an array
logarithmic timemuch fastertakes an amount of time proportional to the logarithm of NK * log(N)Binary Search
constant timefastesttakes a fixed amount of time, no matter how large the input isKArray index lookup
名称速度描述公式例子
factorial time更慢花费的时间与 N 的阶成成正比N!Brute force solution to Traveling Salesman Problem
exponential time花费的时间与某个常数的 N 次幂成正比KNBrute force solution to Rubik's Cube
polynomial time花费的时间与 N 的某个常数幂成正比NK比较排序算法 (bubble, insertion, selection sort)
linearithmic time更快花费的时间介于 linear time 和 polynomial time 之间N * log(N)线性排序算法 (quicksort, heapsort, mergesort)
linear time很快花费的时间与 N 直接成正比K * N数组迭代
logarithmic time非常快花费的时间与 N 的对数成正比K * log(N)二分搜索
constant time最快花费因定时间,与输入大小无关K数组索引查询
更慢<慢<快<更快<很快<非常快<最快

[编辑] Complexity Analysis (复杂度分析)

A given operation can have different time complexities with different orders/sets of input. The different methods of time complexity analysis are as follows:

一个给定的操作,如果输入的顺序/集合不同,那么它的时间复杂度也会不同。下面是一些时间复杂度分析的方法:

NameDescriptionExample
best-caseA case where the operation executes as fast as it possibly canBubblesort has a best-case time complexity of N.
average-caseA case where the operation executes in a time comparable to the majority of possible casesQuicksort has an average-case time complexity of N * log(N)
worst-caseA case where the operation executes as slowly as it possibly canQuicksort has a worst-case time complexity of N2
amortized worst-caseThe average worst-case taken over an infinite number of inputsvector::push_back() has an amortized worst-case time complexity of K (constant time)
名称描述例子
best-case操作可以尽快执行的情况Bubblesort 有一个 N 时间复杂度的 best-case
average-case大部分可能的操作所在的一个执行时间范围的情况Quicksort 有一个 N * log(N) 时间复杂度的的 average-case
worst-case操作会尽量慢执行的情况Quicksort 有一个 N2 时间复杂度的 worst-case
amortized worst-case无限次输入会发生 average worst-case 的情况vector::push_back() 有一个 K (constant time) 时间复杂度的 amortized worst-case

Choosing the right algorithm depends upon which cases you expect your application to encounter. For example, an application that must protect itself from malicious input will avoid naive implementations of quicksort, which has a worst-case time complexity of N2 despite having one of the fastest average-case time complexities compared to all other sorts.

选择正确的算法依赖于你的应用遇到的具体情况。例如,某个应用必须阻止无效输入来避免 quicksort 运行,因为它有一个 N2 时间复杂度的 worst-case,尽管相对其他排序,它的 average-case 时间复杂度是最快中的一个。