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-01-04 08:09:22 +08:00
|
|
|
|
#include "../include/include.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-01-03 21:19:11 +08:00
|
|
|
|
count ++;
|
|
|
|
|
}
|
|
|
|
|
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-01-03 21:19:11 +08:00
|
|
|
|
count ++;
|
|
|
|
|
}
|
|
|
|
|
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-01-03 21:19:11 +08:00
|
|
|
|
count ++;
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 08:09:22 +08:00
|
|
|
|
/* 平方阶 */
|
2023-01-03 21:19:11 +08:00
|
|
|
|
int quadratic(int n)
|
|
|
|
|
{
|
|
|
|
|
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-01-03 21:19:11 +08:00
|
|
|
|
count ++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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-01-03 21:19:11 +08:00
|
|
|
|
int count = 0; // 计数器
|
|
|
|
|
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
2023-01-05 08:39:09 +08:00
|
|
|
|
for (int i = n - 1; i > 0; i--) {
|
2023-01-03 21:19:11 +08:00
|
|
|
|
// 内循环:冒泡操作
|
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;
|
|
|
|
|
count += 3; // 元素交换包含 3 个单元操作
|
|
|
|
|
}
|
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;
|
|
|
|
|
// cell 每轮一分为二,形成数列 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-01-03 21:19:11 +08:00
|
|
|
|
if (n == 1) return 1;
|
|
|
|
|
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-01-03 21:19:11 +08:00
|
|
|
|
if (n <= 1) return 0;
|
|
|
|
|
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-01-03 21:19:11 +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-01-03 21:19:11 +08:00
|
|
|
|
count ++;
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 08:09:22 +08:00
|
|
|
|
/* 阶乘阶(递归实现) */
|
2023-01-05 08:39:09 +08:00
|
|
|
|
int factorialRecur(int n) {
|
2023-01-03 21:19:11 +08:00
|
|
|
|
if (n == 0) return 1;
|
|
|
|
|
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);
|
|
|
|
|
printf("常数阶的计算操作数量 = %d\n", count);
|
|
|
|
|
|
|
|
|
|
count = linear(n);
|
|
|
|
|
printf("线性阶的计算操作数量 = %d\n", count);
|
|
|
|
|
// 分配堆区内存(创建一维可变长数组:数组中元素数量为n,元素类型为int)
|
|
|
|
|
int *nums = (int *)malloc(n * sizeof(int));
|
|
|
|
|
count = arrayTraversal(nums, n);
|
|
|
|
|
printf("线性阶(遍历数组)的计算操作数量 = %d\n", count);
|
|
|
|
|
|
|
|
|
|
count = quadratic(n);
|
|
|
|
|
printf("平方阶的计算操作数量 = %d\n", count);
|
2023-01-05 08:39:09 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-01-03 21:19:11 +08:00
|
|
|
|
nums[i] = n - i; // [n,n-1,...,2,1]
|
|
|
|
|
}
|
|
|
|
|
count = bubbleSort(nums, n);
|
|
|
|
|
printf("平方阶(冒泡排序)的计算操作数量 = %d\n", count);
|
|
|
|
|
|
|
|
|
|
count = exponential(n);
|
|
|
|
|
printf("指数阶(循环实现)的计算操作数量 = %d\n", count);
|
|
|
|
|
count = expRecur(n);
|
|
|
|
|
printf("指数阶(递归实现)的计算操作数量 = %d\n", count);
|
|
|
|
|
|
|
|
|
|
count = logarithmic(n);
|
|
|
|
|
printf("对数阶(循环实现)的计算操作数量 = %d\n", count);
|
|
|
|
|
count = logRecur(n);
|
|
|
|
|
printf("对数阶(递归实现)的计算操作数量 = %d\n", count);
|
|
|
|
|
|
|
|
|
|
count = linearLogRecur(n);
|
|
|
|
|
printf("线性对数阶(递归实现)的计算操作数量 = %d\n", count);
|
|
|
|
|
|
|
|
|
|
count = factorialRecur(n);
|
|
|
|
|
printf("阶乘阶(递归实现)的计算操作数量 = %d\n", count);
|
|
|
|
|
|
|
|
|
|
// 释放堆区内存
|
2023-01-05 08:39:09 +08:00
|
|
|
|
if (nums != NULL) {
|
2023-01-03 21:19:11 +08:00
|
|
|
|
free(nums);
|
|
|
|
|
nums = NULL;
|
|
|
|
|
}
|
|
|
|
|
getchar();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|