2023-01-06 03:39:19 +08:00
|
|
|
|
/**
|
2023-01-04 08:09:22 +08:00
|
|
|
|
* File: time_complexity.c
|
|
|
|
|
* Created Time: 2023-01-03
|
|
|
|
|
* Author: sjinzh (sjinzh@gmail.com)
|
|
|
|
|
*/
|
2023-01-03 21:19:11 +08:00
|
|
|
|
|
2023-04-24 04:11:18 +08:00
|
|
|
|
#include "../utils/common.h"
|
2023-01-03 21:19:11 +08:00
|
|
|
|
|
2023-01-04 08:09:22 +08:00
|
|
|
|
/* 常数阶 */
|
2023-01-05 08:39:09 +08:00
|
|
|
|
int constant(int n) {
|
2023-01-03 21:19:11 +08:00
|
|
|
|
int count = 0;
|
|
|
|
|
int size = 100000;
|
|
|
|
|
int i = 0;
|
2023-01-05 08:39:09 +08:00
|
|
|
|
for (int i = 0; i < size; i++) {
|
2023-04-17 21:13:15 +08:00
|
|
|
|
count++;
|
2023-01-03 21:19:11 +08:00
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 08:09:22 +08:00
|
|
|
|
/* 线性阶 */
|
2023-01-05 08:39:09 +08:00
|
|
|
|
int linear(int n) {
|
2023-01-03 21:19:11 +08:00
|
|
|
|
int count = 0;
|
2023-01-05 08:39:09 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-04-17 21:13:15 +08:00
|
|
|
|
count++;
|
2023-01-03 21:19:11 +08:00
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 08:09:22 +08:00
|
|
|
|
/* 线性阶(遍历数组) */
|
2023-01-05 08:39:09 +08:00
|
|
|
|
int arrayTraversal(int *nums, int n) {
|
2023-01-03 21:19:11 +08:00
|
|
|
|
int count = 0;
|
|
|
|
|
// 循环次数与数组长度成正比
|
2023-01-05 08:39:09 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-04-17 21:13:15 +08:00
|
|
|
|
count++;
|
2023-01-03 21:19:11 +08:00
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 08:09:22 +08:00
|
|
|
|
/* 平方阶 */
|
2023-04-17 21:13:15 +08:00
|
|
|
|
int quadratic(int n) {
|
2023-01-03 21:19:11 +08:00
|
|
|
|
int count = 0;
|
|
|
|
|
// 循环次数与数组长度成平方关系
|
2023-01-05 08:39:09 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
for (int j = 0; j < n; j++) {
|
2023-04-17 21:13:15 +08:00
|
|
|
|
count++;
|
2023-01-03 21:19:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 08:09:22 +08:00
|
|
|
|
/* 平方阶(冒泡排序) */
|
2023-01-05 08:39:09 +08:00
|
|
|
|
int bubbleSort(int *nums, int n) {
|
2023-04-17 21:13:15 +08:00
|
|
|
|
int count = 0; // 计数器
|
2023-05-22 23:05:37 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-01-05 08:39:09 +08:00
|
|
|
|
for (int i = n - 1; i > 0; i--) {
|
2023-09-02 23:54:38 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-01-05 08:39:09 +08:00
|
|
|
|
for (int j = 0; j < i; j++) {
|
2023-01-10 19:29:57 +08:00
|
|
|
|
if (nums[j] > nums[j + 1]) {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
int tmp = nums[j];
|
|
|
|
|
nums[j] = nums[j + 1];
|
|
|
|
|
nums[j + 1] = tmp;
|
2023-04-17 21:13:15 +08:00
|
|
|
|
count += 3; // 元素交换包含 3 个单元操作
|
2023-01-10 19:29:57 +08:00
|
|
|
|
}
|
2023-01-03 21:19:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 08:09:22 +08:00
|
|
|
|
/* 指数阶(循环实现) */
|
2023-01-05 08:39:09 +08:00
|
|
|
|
int exponential(int n) {
|
2023-01-03 21:19:11 +08:00
|
|
|
|
int count = 0;
|
|
|
|
|
int bas = 1;
|
2023-08-08 23:16:33 +08:00
|
|
|
|
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
2023-01-05 08:39:09 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
for (int j = 0; j < bas; j++) {
|
2023-01-03 21:19:11 +08:00
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
bas *= 2;
|
|
|
|
|
}
|
|
|
|
|
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 08:09:22 +08:00
|
|
|
|
/* 指数阶(递归实现) */
|
2023-01-05 08:39:09 +08:00
|
|
|
|
int expRecur(int n) {
|
2023-04-17 21:13:15 +08:00
|
|
|
|
if (n == 1)
|
|
|
|
|
return 1;
|
2023-01-03 21:19:11 +08:00
|
|
|
|
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 08:09:22 +08:00
|
|
|
|
/* 对数阶(循环实现) */
|
2023-01-05 08:39:09 +08:00
|
|
|
|
int logarithmic(float n) {
|
2023-01-03 21:19:11 +08:00
|
|
|
|
int count = 0;
|
2023-01-05 08:39:09 +08:00
|
|
|
|
while (n > 1) {
|
2023-01-03 21:19:11 +08:00
|
|
|
|
n = n / 2;
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 08:09:22 +08:00
|
|
|
|
/* 对数阶(递归实现) */
|
2023-01-05 08:39:09 +08:00
|
|
|
|
int logRecur(float n) {
|
2023-04-17 21:13:15 +08:00
|
|
|
|
if (n <= 1)
|
|
|
|
|
return 0;
|
2023-01-03 21:19:11 +08:00
|
|
|
|
return logRecur(n / 2) + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 08:09:22 +08:00
|
|
|
|
/* 线性对数阶 */
|
2023-01-05 08:39:09 +08:00
|
|
|
|
int linearLogRecur(float n) {
|
2023-04-17 21:13:15 +08:00
|
|
|
|
if (n <= 1)
|
|
|
|
|
return 1;
|
|
|
|
|
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
|
2023-01-05 08:39:09 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-04-17 21:13:15 +08:00
|
|
|
|
count++;
|
2023-01-03 21:19:11 +08:00
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 08:09:22 +08:00
|
|
|
|
/* 阶乘阶(递归实现) */
|
2023-01-05 08:39:09 +08:00
|
|
|
|
int factorialRecur(int n) {
|
2023-04-17 21:13:15 +08:00
|
|
|
|
if (n == 0)
|
|
|
|
|
return 1;
|
2023-01-03 21:19:11 +08:00
|
|
|
|
int count = 0;
|
2023-01-05 08:39:09 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-01-03 21:19:11 +08:00
|
|
|
|
count += factorialRecur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 08:09:22 +08:00
|
|
|
|
/* Driver Code */
|
2023-01-05 08:39:09 +08:00
|
|
|
|
int main(int argc, char *argv[]) {
|
2023-01-03 21:19:11 +08:00
|
|
|
|
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
|
|
|
|
|
int n = 8;
|
|
|
|
|
printf("输入数据大小 n = %d\n", n);
|
|
|
|
|
|
|
|
|
|
int count = constant(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
printf("常数阶的操作数量 = %d\n", count);
|
2023-01-03 21:19:11 +08:00
|
|
|
|
|
|
|
|
|
count = linear(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
printf("线性阶的操作数量 = %d\n", count);
|
2023-01-03 21:19:11 +08:00
|
|
|
|
// 分配堆区内存(创建一维可变长数组:数组中元素数量为n,元素类型为int)
|
|
|
|
|
int *nums = (int *)malloc(n * sizeof(int));
|
|
|
|
|
count = arrayTraversal(nums, n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
printf("线性阶(遍历数组)的操作数量 = %d\n", count);
|
2023-01-03 21:19:11 +08:00
|
|
|
|
|
|
|
|
|
count = quadratic(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
printf("平方阶的操作数量 = %d\n", count);
|
2023-01-05 08:39:09 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-04-17 21:13:15 +08:00
|
|
|
|
nums[i] = n - i; // [n,n-1,...,2,1]
|
2023-01-03 21:19:11 +08:00
|
|
|
|
}
|
|
|
|
|
count = bubbleSort(nums, n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
printf("平方阶(冒泡排序)的操作数量 = %d\n", count);
|
2023-01-03 21:19:11 +08:00
|
|
|
|
|
|
|
|
|
count = exponential(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
printf("指数阶(循环实现)的操作数量 = %d\n", count);
|
2023-01-03 21:19:11 +08:00
|
|
|
|
count = expRecur(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
printf("指数阶(递归实现)的操作数量 = %d\n", count);
|
2023-01-03 21:19:11 +08:00
|
|
|
|
|
|
|
|
|
count = logarithmic(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
printf("对数阶(循环实现)的操作数量 = %d\n", count);
|
2023-01-03 21:19:11 +08:00
|
|
|
|
count = logRecur(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
printf("对数阶(递归实现)的操作数量 = %d\n", count);
|
2023-01-03 21:19:11 +08:00
|
|
|
|
|
|
|
|
|
count = linearLogRecur(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
printf("线性对数阶(递归实现)的操作数量 = %d\n", count);
|
2023-01-03 21:19:11 +08:00
|
|
|
|
|
|
|
|
|
count = factorialRecur(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
|
printf("阶乘阶(递归实现)的操作数量 = %d\n", count);
|
2023-01-03 21:19:11 +08:00
|
|
|
|
|
|
|
|
|
// 释放堆区内存
|
2023-01-05 08:39:09 +08:00
|
|
|
|
if (nums != NULL) {
|
2023-01-03 21:19:11 +08:00
|
|
|
|
free(nums);
|
|
|
|
|
nums = NULL;
|
|
|
|
|
}
|
|
|
|
|
getchar();
|
2023-04-17 21:13:15 +08:00
|
|
|
|
|
2023-01-03 21:19:11 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|