2023-01-06 03:39:19 +08:00
|
|
|
/**
|
2022-12-13 23:24:12 +08:00
|
|
|
* File: time_complexity.java
|
2022-11-25 02:04:38 +08:00
|
|
|
* Created Time: 2022-11-25
|
|
|
|
* Author: Krahets (krahets@163.com)
|
|
|
|
*/
|
|
|
|
|
2022-11-12 22:48:46 +08:00
|
|
|
package chapter_computational_complexity;
|
2022-11-08 02:58:42 +08:00
|
|
|
|
2022-12-13 23:24:12 +08:00
|
|
|
public class time_complexity {
|
2022-11-08 02:58:42 +08:00
|
|
|
/* 常数阶 */
|
|
|
|
static int constant(int n) {
|
|
|
|
int count = 0;
|
|
|
|
int size = 100000;
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
count++;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 线性阶 */
|
|
|
|
static int linear(int n) {
|
|
|
|
int count = 0;
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
count++;
|
|
|
|
return count;
|
|
|
|
}
|
2023-04-14 00:12:10 +08:00
|
|
|
|
2022-11-08 02:58:42 +08:00
|
|
|
/* 线性阶(遍历数组) */
|
|
|
|
static int arrayTraversal(int[] nums) {
|
|
|
|
int count = 0;
|
|
|
|
// 循环次数与数组长度成正比
|
|
|
|
for (int num : nums) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 平方阶 */
|
|
|
|
static int quadratic(int n) {
|
|
|
|
int count = 0;
|
2022-11-25 20:12:20 +08:00
|
|
|
// 循环次数与数组长度成平方关系
|
2022-11-08 02:58:42 +08:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
for (int j = 0; j < n; j++) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 平方阶(冒泡排序) */
|
2022-11-25 20:12:20 +08:00
|
|
|
static int bubbleSort(int[] nums) {
|
2023-04-14 00:12:10 +08:00
|
|
|
int count = 0; // 计数器
|
2023-05-22 23:05:37 +08:00
|
|
|
// 外循环:未排序区间为 [0, i]
|
2022-11-25 20:12:20 +08:00
|
|
|
for (int i = nums.length - 1; i > 0; i--) {
|
2023-09-02 23:54:38 +08:00
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2022-11-25 20:12:20 +08:00
|
|
|
for (int j = 0; j < i; j++) {
|
2022-11-08 02:58:42 +08:00
|
|
|
if (nums[j] > nums[j + 1]) {
|
2022-11-25 20:12:20 +08:00
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
2022-11-08 02:58:42 +08:00
|
|
|
int tmp = nums[j];
|
|
|
|
nums[j] = nums[j + 1];
|
|
|
|
nums[j + 1] = tmp;
|
2023-04-14 00:12:10 +08:00
|
|
|
count += 3; // 元素交换包含 3 个单元操作
|
2022-11-08 02:58:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-25 20:12:20 +08:00
|
|
|
return count;
|
2022-11-08 02:58:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 指数阶(循环实现) */
|
|
|
|
static int exponential(int n) {
|
|
|
|
int count = 0, base = 1;
|
2023-08-08 23:16:33 +08:00
|
|
|
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
2022-11-08 02:58:42 +08:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
for (int j = 0; j < base; j++) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
base *= 2;
|
|
|
|
}
|
|
|
|
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 指数阶(递归实现) */
|
|
|
|
static int expRecur(int n) {
|
2023-04-14 00:12:10 +08:00
|
|
|
if (n == 1)
|
|
|
|
return 1;
|
2022-11-08 02:58:42 +08:00
|
|
|
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 对数阶(循环实现) */
|
|
|
|
static int logarithmic(float n) {
|
|
|
|
int count = 0;
|
|
|
|
while (n > 1) {
|
|
|
|
n = n / 2;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 对数阶(递归实现) */
|
|
|
|
static int logRecur(float n) {
|
2023-04-14 00:12:10 +08:00
|
|
|
if (n <= 1)
|
|
|
|
return 0;
|
2022-11-08 02:58:42 +08:00
|
|
|
return logRecur(n / 2) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 线性对数阶 */
|
|
|
|
static int linearLogRecur(float n) {
|
2023-04-14 00:12:10 +08:00
|
|
|
if (n <= 1)
|
|
|
|
return 1;
|
|
|
|
int count = linearLogRecur(n / 2) +
|
|
|
|
linearLogRecur(n / 2);
|
2022-11-08 02:58:42 +08:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 阶乘阶(递归实现) */
|
|
|
|
static int factorialRecur(int n) {
|
2023-04-14 00:12:10 +08:00
|
|
|
if (n == 0)
|
|
|
|
return 1;
|
2022-11-08 02:58:42 +08:00
|
|
|
int count = 0;
|
|
|
|
// 从 1 个分裂出 n 个
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
count += factorialRecur(n - 1);
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
public static void main(String[] args) {
|
|
|
|
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
|
|
|
|
int n = 8;
|
|
|
|
System.out.println("输入数据大小 n = " + n);
|
|
|
|
|
|
|
|
int count = constant(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
System.out.println("常数阶的操作数量 = " + count);
|
2022-11-08 02:58:42 +08:00
|
|
|
|
|
|
|
count = linear(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
System.out.println("线性阶的操作数量 = " + count);
|
2022-11-08 02:58:42 +08:00
|
|
|
count = arrayTraversal(new int[n]);
|
2023-08-20 14:51:39 +08:00
|
|
|
System.out.println("线性阶(遍历数组)的操作数量 = " + count);
|
2022-11-08 02:58:42 +08:00
|
|
|
|
|
|
|
count = quadratic(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
System.out.println("平方阶的操作数量 = " + count);
|
2022-11-25 20:12:20 +08:00
|
|
|
int[] nums = new int[n];
|
|
|
|
for (int i = 0; i < n; i++)
|
2023-04-14 00:12:10 +08:00
|
|
|
nums[i] = n - i; // [n,n-1,...,2,1]
|
2022-11-25 20:12:20 +08:00
|
|
|
count = bubbleSort(nums);
|
2023-08-20 14:51:39 +08:00
|
|
|
System.out.println("平方阶(冒泡排序)的操作数量 = " + count);
|
2022-11-08 02:58:42 +08:00
|
|
|
|
|
|
|
count = exponential(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
System.out.println("指数阶(循环实现)的操作数量 = " + count);
|
2022-11-08 02:58:42 +08:00
|
|
|
count = expRecur(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
System.out.println("指数阶(递归实现)的操作数量 = " + count);
|
2022-11-08 02:58:42 +08:00
|
|
|
|
|
|
|
count = logarithmic((float) n);
|
2023-08-20 14:51:39 +08:00
|
|
|
System.out.println("对数阶(循环实现)的操作数量 = " + count);
|
2022-11-08 02:58:42 +08:00
|
|
|
count = logRecur((float) n);
|
2023-08-20 14:51:39 +08:00
|
|
|
System.out.println("对数阶(递归实现)的操作数量 = " + count);
|
2022-11-08 02:58:42 +08:00
|
|
|
|
|
|
|
count = linearLogRecur((float) n);
|
2023-08-20 14:51:39 +08:00
|
|
|
System.out.println("线性对数阶(递归实现)的操作数量 = " + count);
|
2022-11-08 02:58:42 +08:00
|
|
|
|
|
|
|
count = factorialRecur(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
System.out.println("阶乘阶(递归实现)的操作数量 = " + count);
|
2022-11-08 02:58:42 +08:00
|
|
|
}
|
|
|
|
}
|