2023-01-06 03:39:19 +08:00
|
|
|
|
/**
|
2022-12-26 23:29:37 +08:00
|
|
|
|
* File: time_complexity.swift
|
|
|
|
|
* Created Time: 2022-12-26
|
|
|
|
|
* Author: nuomi1 (nuomi1@qq.com)
|
|
|
|
|
*/
|
|
|
|
|
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* 常数阶 */
|
2022-12-26 23:29:37 +08:00
|
|
|
|
func constant(n: Int) -> Int {
|
|
|
|
|
var count = 0
|
|
|
|
|
let size = 100_000
|
|
|
|
|
for _ in 0 ..< size {
|
|
|
|
|
count += 1
|
|
|
|
|
}
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* 线性阶 */
|
2022-12-26 23:29:37 +08:00
|
|
|
|
func linear(n: Int) -> Int {
|
|
|
|
|
var count = 0
|
|
|
|
|
for _ in 0 ..< n {
|
|
|
|
|
count += 1
|
|
|
|
|
}
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* 线性阶(遍历数组) */
|
2022-12-26 23:29:37 +08:00
|
|
|
|
func arrayTraversal(nums: [Int]) -> Int {
|
|
|
|
|
var count = 0
|
|
|
|
|
// 循环次数与数组长度成正比
|
|
|
|
|
for _ in nums {
|
|
|
|
|
count += 1
|
|
|
|
|
}
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* 平方阶 */
|
2022-12-26 23:29:37 +08:00
|
|
|
|
func quadratic(n: Int) -> Int {
|
|
|
|
|
var count = 0
|
|
|
|
|
// 循环次数与数组长度成平方关系
|
|
|
|
|
for _ in 0 ..< n {
|
|
|
|
|
for _ in 0 ..< n {
|
|
|
|
|
count += 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* 平方阶(冒泡排序) */
|
2022-12-26 23:29:37 +08:00
|
|
|
|
func bubbleSort(nums: inout [Int]) -> Int {
|
|
|
|
|
var count = 0 // 计数器
|
|
|
|
|
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
2023-01-08 23:48:35 +08:00
|
|
|
|
for i in sequence(first: nums.count - 1, next: { $0 > 0 + 1 ? $0 - 1 : nil }) {
|
2022-12-26 23:29:37 +08:00
|
|
|
|
// 内循环:冒泡操作
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* 指数阶(循环实现) */
|
2022-12-26 23:29:37 +08:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* 指数阶(递归实现) */
|
2022-12-26 23:29:37 +08:00
|
|
|
|
func expRecur(n: Int) -> Int {
|
|
|
|
|
if n == 1 {
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
return expRecur(n: n - 1) + expRecur(n: n - 1) + 1
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* 对数阶(循环实现) */
|
2022-12-26 23:29:37 +08:00
|
|
|
|
func logarithmic(n: Int) -> Int {
|
|
|
|
|
var count = 0
|
|
|
|
|
var n = n
|
|
|
|
|
while n > 1 {
|
|
|
|
|
n = n / 2
|
|
|
|
|
count += 1
|
|
|
|
|
}
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* 对数阶(递归实现) */
|
2022-12-26 23:29:37 +08:00
|
|
|
|
func logRecur(n: Int) -> Int {
|
|
|
|
|
if n <= 1 {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
return logRecur(n: n / 2) + 1
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* 线性对数阶 */
|
2022-12-26 23:29:37 +08:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* 阶乘阶(递归实现) */
|
2022-12-26 23:29:37 +08:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-02 21:48:32 +08:00
|
|
|
|
@main
|
|
|
|
|
enum TimeComplexity {
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* Driver Code */
|
2023-01-02 21:48:32 +08:00
|
|
|
|
static func main() {
|
|
|
|
|
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
|
|
|
|
|
let n = 8
|
2023-01-08 20:54:40 +08:00
|
|
|
|
print("输入数据大小 n = \(n)")
|
2023-01-02 21:48:32 +08:00
|
|
|
|
|
|
|
|
|
var count = constant(n: n)
|
2023-01-08 20:54:40 +08:00
|
|
|
|
print("常数阶的计算操作数量 = \(count)")
|
2023-01-02 21:48:32 +08:00
|
|
|
|
|
|
|
|
|
count = linear(n: n)
|
2023-01-08 20:54:40 +08:00
|
|
|
|
print("线性阶的计算操作数量 = \(count)")
|
2023-01-02 21:48:32 +08:00
|
|
|
|
count = arrayTraversal(nums: Array(repeating: 0, count: n))
|
2023-01-08 20:54:40 +08:00
|
|
|
|
print("线性阶(遍历数组)的计算操作数量 = \(count)")
|
2023-01-02 21:48:32 +08:00
|
|
|
|
|
|
|
|
|
count = quadratic(n: n)
|
2023-01-08 20:54:40 +08:00
|
|
|
|
print("平方阶的计算操作数量 = \(count)")
|
2023-01-08 23:48:35 +08:00
|
|
|
|
var nums = Array(sequence(first: n, next: { $0 > 0 + 1 ? $0 - 1 : nil })) // [n,n-1,...,2,1]
|
2023-01-02 21:48:32 +08:00
|
|
|
|
count = bubbleSort(nums: &nums)
|
2023-01-08 20:54:40 +08:00
|
|
|
|
print("平方阶(冒泡排序)的计算操作数量 = \(count)")
|
2023-01-02 21:48:32 +08:00
|
|
|
|
|
|
|
|
|
count = exponential(n: n)
|
2023-01-08 20:54:40 +08:00
|
|
|
|
print("指数阶(循环实现)的计算操作数量 = \(count)")
|
2023-01-02 21:48:32 +08:00
|
|
|
|
count = expRecur(n: n)
|
2023-01-08 20:54:40 +08:00
|
|
|
|
print("指数阶(递归实现)的计算操作数量 = \(count)")
|
2023-01-02 21:48:32 +08:00
|
|
|
|
|
|
|
|
|
count = logarithmic(n: n)
|
2023-01-08 20:54:40 +08:00
|
|
|
|
print("对数阶(循环实现)的计算操作数量 = \(count)")
|
2023-01-02 21:48:32 +08:00
|
|
|
|
count = logRecur(n: n)
|
2023-01-08 20:54:40 +08:00
|
|
|
|
print("对数阶(递归实现)的计算操作数量 = \(count)")
|
2023-01-02 21:48:32 +08:00
|
|
|
|
|
|
|
|
|
count = linearLogRecur(n: Double(n))
|
2023-01-08 20:54:40 +08:00
|
|
|
|
print("线性对数阶(递归实现)的计算操作数量 = \(count)")
|
2023-01-02 21:48:32 +08:00
|
|
|
|
|
|
|
|
|
count = factorialRecur(n: n)
|
2023-01-08 20:54:40 +08:00
|
|
|
|
print("阶乘阶(递归实现)的计算操作数量 = \(count)")
|
2023-01-02 21:48:32 +08:00
|
|
|
|
}
|
2022-12-26 23:29:37 +08:00
|
|
|
|
}
|