|
|
|
@ -111,6 +111,21 @@ $$
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title=""
|
|
|
|
|
// 在某运行平台下
|
|
|
|
|
func algorithm(_ n: Int) {
|
|
|
|
|
var a = 2 // 1 ns
|
|
|
|
|
a = a + 1 // 1 ns
|
|
|
|
|
a = a * 2 // 10 ns
|
|
|
|
|
// 循环 n 次
|
|
|
|
|
for _ in 0 ..< n { // 1 ns
|
|
|
|
|
print(0) // 5 ns
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
但实际上, **统计算法的运行时间既不合理也不现实。** 首先,我们不希望预估时间和运行平台绑定,毕竟算法需要跑在各式各样的平台之上。其次,我们很难获知每一种操作的运行时间,这为预估过程带来了极大的难度。
|
|
|
|
|
|
|
|
|
|
## 统计时间增长趋势
|
|
|
|
@ -246,6 +261,29 @@ $$
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title=""
|
|
|
|
|
// 算法 A 时间复杂度:常数阶
|
|
|
|
|
func algorithmA(_ n: Int) {
|
|
|
|
|
print(0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 算法 B 时间复杂度:线性阶
|
|
|
|
|
func algorithmB(_ n: Int) {
|
|
|
|
|
for _ in 0 ..< n {
|
|
|
|
|
print(0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 算法 C 时间复杂度:常数阶
|
|
|
|
|
func algorithmC(_ n: Int) {
|
|
|
|
|
for _ in 0 ..< 1000000 {
|
|
|
|
|
print(0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
![time_complexity_first_example](time_complexity.assets/time_complexity_first_example.png)
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 算法 A, B, C 的时间增长趋势 </p>
|
|
|
|
@ -352,6 +390,20 @@ $$
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title=""
|
|
|
|
|
func algorithm(n: Int) {
|
|
|
|
|
var a = 1 // +1
|
|
|
|
|
a = a + 1 // +1
|
|
|
|
|
a = a * 2 // +1
|
|
|
|
|
// 循环 n 次
|
|
|
|
|
for _ in 0 ..< n { // +1
|
|
|
|
|
print(0) // +1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
$T(n)$ 是个一次函数,说明时间增长趋势是线性的,因此易得时间复杂度是线性阶。
|
|
|
|
|
|
|
|
|
|
我们将线性阶的时间复杂度记为 $O(n)$ ,这个数学符号被称为「大 $O$ 记号 Big-$O$ Notation」,代表函数 $T(n)$ 的「渐近上界 asymptotic upper bound」。
|
|
|
|
@ -516,6 +568,25 @@ $$
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title=""
|
|
|
|
|
func algorithm(n: Int) {
|
|
|
|
|
var a = 1 // +0(技巧 1)
|
|
|
|
|
a = a + n // +0(技巧 1)
|
|
|
|
|
// +n(技巧 2)
|
|
|
|
|
for _ in 0 ..< (5 * n + 1) {
|
|
|
|
|
print(0)
|
|
|
|
|
}
|
|
|
|
|
// +n*n(技巧 3)
|
|
|
|
|
for _ in 0 ..< (2 * n) {
|
|
|
|
|
for _ in 0 ..< (n + 1) {
|
|
|
|
|
print(0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 2. 判断渐近上界
|
|
|
|
|
|
|
|
|
|
**时间复杂度由多项式 $T(n)$ 中最高阶的项来决定**。这是因为在 $n$ 趋于无穷大时,最高阶的项将处于主导作用,其它项的影响都可以被忽略。
|
|
|
|
@ -643,6 +714,20 @@ $$
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="time_complexity.swift"
|
|
|
|
|
// 常数阶
|
|
|
|
|
func constant(n: Int) -> Int {
|
|
|
|
|
var count = 0
|
|
|
|
|
let size = 100000
|
|
|
|
|
for _ in 0 ..< size {
|
|
|
|
|
count += 1
|
|
|
|
|
}
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 线性阶 $O(n)$
|
|
|
|
|
|
|
|
|
|
线性阶的操作数量相对输入数据大小成线性级别增长。线性阶常出现于单层循环。
|
|
|
|
@ -726,6 +811,19 @@ $$
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="time_complexity.swift"
|
|
|
|
|
// 线性阶
|
|
|
|
|
func linear(n: Int) -> Int {
|
|
|
|
|
var count = 0
|
|
|
|
|
for _ in 0 ..< n {
|
|
|
|
|
count += 1
|
|
|
|
|
}
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
「遍历数组」和「遍历链表」等操作,时间复杂度都为 $O(n)$ ,其中 $n$ 为数组或链表的长度。
|
|
|
|
|
|
|
|
|
|
!!! tip
|
|
|
|
@ -820,6 +918,20 @@ $$
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="time_complexity.swift"
|
|
|
|
|
// 线性阶(遍历数组)
|
|
|
|
|
func arrayTraversal(nums: [Int]) -> Int {
|
|
|
|
|
var count = 0
|
|
|
|
|
// 循环次数与数组长度成正比
|
|
|
|
|
for _ in nums {
|
|
|
|
|
count += 1
|
|
|
|
|
}
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 平方阶 $O(n^2)$
|
|
|
|
|
|
|
|
|
|
平方阶的操作数量相对输入数据大小成平方级别增长。平方阶常出现于嵌套循环,外层循环和内层循环都为 $O(n)$ ,总体为 $O(n^2)$ 。
|
|
|
|
@ -922,6 +1034,22 @@ $$
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="time_complexity.swift"
|
|
|
|
|
// 平方阶
|
|
|
|
|
func quadratic(n: Int) -> Int {
|
|
|
|
|
var count = 0
|
|
|
|
|
// 循环次数与数组长度成平方关系
|
|
|
|
|
for _ in 0 ..< n {
|
|
|
|
|
for _ in 0 ..< n {
|
|
|
|
|
count += 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
![time_complexity_constant_linear_quadratic](time_complexity.assets/time_complexity_constant_linear_quadratic.png)
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 常数阶、线性阶、平方阶的时间复杂度 </p>
|
|
|
|
@ -1066,6 +1194,29 @@ $$
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="time_complexity.swift"
|
|
|
|
|
// 平方阶(冒泡排序)
|
|
|
|
|
func bubbleSort(nums: inout [Int]) -> Int {
|
|
|
|
|
var count = 0 // 计数器
|
|
|
|
|
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
|
|
|
|
for i in sequence(first: nums.count - 1, next: { $0 > 0 ? $0 - 1 : nil }) {
|
|
|
|
|
// 内循环:冒泡操作
|
|
|
|
|
for j in 0 ..< i {
|
|
|
|
|
if nums[j] > nums[j + 1] {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
let tmp = nums[j]
|
|
|
|
|
nums[j] = nums[j + 1]
|
|
|
|
|
nums[j + 1] = tmp
|
|
|
|
|
count += 3 // 元素交换包含 3 个单元操作
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 指数阶 $O(2^n)$
|
|
|
|
|
|
|
|
|
|
!!! note
|
|
|
|
@ -1182,6 +1333,25 @@ $$
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="time_complexity.swift"
|
|
|
|
|
// 指数阶(循环实现)
|
|
|
|
|
func exponential(n: Int) -> Int {
|
|
|
|
|
var count = 0
|
|
|
|
|
var base = 1
|
|
|
|
|
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
|
|
|
|
for _ in 0 ..< n {
|
|
|
|
|
for _ in 0 ..< base {
|
|
|
|
|
count += 1
|
|
|
|
|
}
|
|
|
|
|
base *= 2
|
|
|
|
|
}
|
|
|
|
|
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
![time_complexity_exponential](time_complexity.assets/time_complexity_exponential.png)
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 指数阶的时间复杂度 </p>
|
|
|
|
@ -1258,6 +1428,18 @@ $$
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="time_complexity.swift"
|
|
|
|
|
// 指数阶(递归实现)
|
|
|
|
|
func expRecur(n: Int) -> Int {
|
|
|
|
|
if n == 1 {
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
return expRecur(n: n - 1) + expRecur(n: n - 1) + 1
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 对数阶 $O(\log n)$
|
|
|
|
|
|
|
|
|
|
对数阶与指数阶正好相反,后者反映“每轮增加到两倍的情况”,而前者反映“每轮缩减到一半的情况”。对数阶仅次于常数阶,时间增长的很慢,是理想的时间复杂度。
|
|
|
|
@ -1354,6 +1536,21 @@ $$
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="time_complexity.swift"
|
|
|
|
|
// 对数阶(循环实现)
|
|
|
|
|
func logarithmic(n: Int) -> Int {
|
|
|
|
|
var count = 0
|
|
|
|
|
var n = n
|
|
|
|
|
while n > 1 {
|
|
|
|
|
n = n / 2
|
|
|
|
|
count += 1
|
|
|
|
|
}
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
![time_complexity_logarithmic](time_complexity.assets/time_complexity_logarithmic.png)
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 对数阶的时间复杂度 </p>
|
|
|
|
@ -1430,6 +1627,18 @@ $$
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="time_complexity.swift"
|
|
|
|
|
// 对数阶(递归实现)
|
|
|
|
|
func logRecur(n: Int) -> Int {
|
|
|
|
|
if n <= 1 {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
return logRecur(n: n / 2) + 1
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 线性对数阶 $O(n \log n)$
|
|
|
|
|
|
|
|
|
|
线性对数阶常出现于嵌套循环中,两层循环的时间复杂度分别为 $O(\log n)$ 和 $O(n)$ 。
|
|
|
|
@ -1531,6 +1740,22 @@ $$
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="time_complexity.swift"
|
|
|
|
|
// 线性对数阶
|
|
|
|
|
func linearLogRecur(n: Double) -> Int {
|
|
|
|
|
if n <= 1 {
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
var count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2)
|
|
|
|
|
for _ in 0 ..< Int(n) {
|
|
|
|
|
count += 1
|
|
|
|
|
}
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
![time_complexity_logarithmic_linear](time_complexity.assets/time_complexity_logarithmic_linear.png)
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 线性对数阶的时间复杂度 </p>
|
|
|
|
@ -1640,6 +1865,23 @@ $$
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="time_complexity.swift"
|
|
|
|
|
// 阶乘阶(递归实现)
|
|
|
|
|
func factorialRecur(n: Int) -> Int {
|
|
|
|
|
if n == 0 {
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
var count = 0
|
|
|
|
|
// 从 1 个分裂出 n 个
|
|
|
|
|
for _ in 0 ..< n {
|
|
|
|
|
count += factorialRecur(n: n - 1)
|
|
|
|
|
}
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
![time_complexity_factorial](time_complexity.assets/time_complexity_factorial.png)
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 阶乘阶的时间复杂度 </p>
|
|
|
|
@ -1872,6 +2114,40 @@ $$
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title=""
|
|
|
|
|
// 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱
|
|
|
|
|
func randomNumbers(n: Int) -> [Int] {
|
|
|
|
|
// 生成数组 nums = { 1, 2, 3, ..., n }
|
|
|
|
|
var nums = Array(1 ... n)
|
|
|
|
|
// 随机打乱数组元素
|
|
|
|
|
nums.shuffle()
|
|
|
|
|
return nums
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查找数组 nums 中数字 1 所在索引
|
|
|
|
|
func findOne(nums: [Int]) -> Int {
|
|
|
|
|
for i in nums.indices {
|
|
|
|
|
if nums[i] == 1 {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Driver Code
|
|
|
|
|
func main() {
|
|
|
|
|
for _ in 0 ..< 10 {
|
|
|
|
|
let n = 100
|
|
|
|
|
let nums = randomNumbers(n: n)
|
|
|
|
|
let index = findOne(nums: nums)
|
|
|
|
|
print("数组 [ 1, 2, ..., n ] 被打乱后 =", nums)
|
|
|
|
|
print("数字 1 的索引为", index)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
!!! tip
|
|
|
|
|
|
|
|
|
|
我们在实际应用中很少使用「最佳时间复杂度」,因为往往只有很小概率下才能达到,会带来一定的误导性。反之,「最差时间复杂度」最为实用,因为它给出了一个“效率安全值”,让我们可以放心地使用算法。
|
|
|
|
|