2022-12-23 18:28:17 +08:00
|
|
|
|
/**
|
|
|
|
|
* File: time_complexity.cs
|
|
|
|
|
* Created Time: 2022-12-23
|
|
|
|
|
* Author: haptear (haptear@hotmail.com)
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
namespace hello_algo.chapter_computational_complexity;
|
|
|
|
|
|
2023-04-23 03:03:12 +08:00
|
|
|
|
public class time_complexity {
|
2023-10-08 01:33:46 +08:00
|
|
|
|
void Algorithm(int n) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
int a = 1; // +0(技巧 1)
|
2023-11-26 23:18:44 +08:00
|
|
|
|
a += n; // +0(技巧 1)
|
2023-02-08 22:18:02 +08:00
|
|
|
|
// +n(技巧 2)
|
2023-04-23 03:03:12 +08:00
|
|
|
|
for (int i = 0; i < 5 * n + 1; i++) {
|
2022-12-23 15:42:02 +08:00
|
|
|
|
Console.WriteLine(0);
|
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
// +n*n(技巧 3)
|
2023-04-23 03:03:12 +08:00
|
|
|
|
for (int i = 0; i < 2 * n; i++) {
|
|
|
|
|
for (int j = 0; j < n + 1; j++) {
|
2022-12-23 15:42:02 +08:00
|
|
|
|
Console.WriteLine(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
// 算法 A 时间复杂度:常数阶
|
2023-10-08 01:33:46 +08:00
|
|
|
|
void AlgorithmA(int n) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
Console.WriteLine(0);
|
|
|
|
|
}
|
2023-11-26 23:18:44 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
// 算法 B 时间复杂度:线性阶
|
2023-10-08 01:33:46 +08:00
|
|
|
|
void AlgorithmB(int n) {
|
2023-04-23 03:03:12 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
Console.WriteLine(0);
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|
2023-11-26 23:18:44 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
// 算法 C 时间复杂度:常数阶
|
2023-10-08 01:33:46 +08:00
|
|
|
|
void AlgorithmC(int n) {
|
2023-04-23 03:03:12 +08:00
|
|
|
|
for (int i = 0; i < 1000000; i++) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
Console.WriteLine(0);
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 常数阶 */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
int Constant(int n) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
int count = 0;
|
|
|
|
|
int size = 100000;
|
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
|
count++;
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 线性阶 */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
int Linear(int n) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
int count = 0;
|
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
|
count++;
|
|
|
|
|
return count;
|
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 线性阶(遍历数组) */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
int ArrayTraversal(int[] nums) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
int count = 0;
|
|
|
|
|
// 循环次数与数组长度成正比
|
2023-04-23 03:03:12 +08:00
|
|
|
|
foreach (int num in nums) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
count++;
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
return count;
|
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 平方阶 */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
int Quadratic(int n) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
int count = 0;
|
2024-03-19 02:28:16 +08:00
|
|
|
|
// 循环次数与数据大小 n 成平方关系
|
2023-04-23 03:03:12 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
for (int j = 0; j < n; j++) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
count++;
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
return count;
|
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 平方阶(冒泡排序) */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
int BubbleSort(int[] nums) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
int count = 0; // 计数器
|
2023-05-22 23:05:37 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-04-23 03:03:12 +08:00
|
|
|
|
for (int i = nums.Length - 1; i > 0; i--) {
|
2024-03-18 13:34:02 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-04-23 03:03:12 +08:00
|
|
|
|
for (int j = 0; j < i; j++) {
|
|
|
|
|
if (nums[j] > nums[j + 1]) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
2023-07-24 19:50:00 +08:00
|
|
|
|
(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
count += 3; // 元素交换包含 3 个单元操作
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
return count;
|
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 指数阶(循环实现) */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
int Exponential(int n) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
int count = 0, bas = 1;
|
2023-08-08 23:16:33 +08:00
|
|
|
|
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
2023-04-23 03:03:12 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
for (int j = 0; j < bas; j++) {
|
2022-12-23 15:42:02 +08:00
|
|
|
|
count++;
|
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
bas *= 2;
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
|
|
|
|
return count;
|
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 指数阶(递归实现) */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
int ExpRecur(int n) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
if (n == 1) return 1;
|
2023-10-08 01:33:46 +08:00
|
|
|
|
return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 对数阶(循环实现) */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
int Logarithmic(float n) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
int count = 0;
|
2023-04-23 03:03:12 +08:00
|
|
|
|
while (n > 1) {
|
2023-11-26 23:18:44 +08:00
|
|
|
|
n /= 2;
|
2023-02-08 22:18:02 +08:00
|
|
|
|
count++;
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
return count;
|
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 对数阶(递归实现) */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
int LogRecur(float n) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
if (n <= 1) return 0;
|
2023-10-08 01:33:46 +08:00
|
|
|
|
return LogRecur(n / 2) + 1;
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 线性对数阶 */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
int LinearLogRecur(float n) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
if (n <= 1) return 1;
|
2023-10-08 20:33:30 +08:00
|
|
|
|
int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);
|
2023-04-23 03:03:12 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
count++;
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
return count;
|
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 阶乘阶(递归实现) */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
int FactorialRecur(int n) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
if (n == 0) return 1;
|
|
|
|
|
int count = 0;
|
|
|
|
|
// 从 1 个分裂出 n 个
|
2023-04-23 03:03:12 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-10-08 01:33:46 +08:00
|
|
|
|
count += FactorialRecur(n - 1);
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2023-04-23 03:03:12 +08:00
|
|
|
|
public void Test() {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
|
|
|
|
|
int n = 8;
|
|
|
|
|
Console.WriteLine("输入数据大小 n = " + n);
|
|
|
|
|
|
2023-10-08 01:33:46 +08:00
|
|
|
|
int count = Constant(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
Console.WriteLine("常数阶的操作数量 = " + count);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
|
2023-10-08 01:33:46 +08:00
|
|
|
|
count = Linear(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
Console.WriteLine("线性阶的操作数量 = " + count);
|
2023-10-08 01:33:46 +08:00
|
|
|
|
count = ArrayTraversal(new int[n]);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
Console.WriteLine("线性阶(遍历数组)的操作数量 = " + count);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
|
2023-10-08 01:33:46 +08:00
|
|
|
|
count = Quadratic(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
Console.WriteLine("平方阶的操作数量 = " + count);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
int[] nums = new int[n];
|
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
|
nums[i] = n - i; // [n,n-1,...,2,1]
|
2023-10-08 01:33:46 +08:00
|
|
|
|
count = BubbleSort(nums);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
Console.WriteLine("平方阶(冒泡排序)的操作数量 = " + count);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
|
2023-10-08 01:33:46 +08:00
|
|
|
|
count = Exponential(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
Console.WriteLine("指数阶(循环实现)的操作数量 = " + count);
|
2023-10-08 01:33:46 +08:00
|
|
|
|
count = ExpRecur(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
Console.WriteLine("指数阶(递归实现)的操作数量 = " + count);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
|
2023-10-08 01:33:46 +08:00
|
|
|
|
count = Logarithmic((float)n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
Console.WriteLine("对数阶(循环实现)的操作数量 = " + count);
|
2023-10-08 01:33:46 +08:00
|
|
|
|
count = LogRecur((float)n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
Console.WriteLine("对数阶(递归实现)的操作数量 = " + count);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
|
2023-10-08 01:33:46 +08:00
|
|
|
|
count = LinearLogRecur((float)n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
Console.WriteLine("线性对数阶(递归实现)的操作数量 = " + count);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
|
2023-10-08 01:33:46 +08:00
|
|
|
|
count = FactorialRecur(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
Console.WriteLine("阶乘阶(递归实现)的操作数量 = " + count);
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|