hello-algo/codes/go/chapter_computational_complexity/time_complexity_test.go

49 lines
1.3 KiB
Go
Raw Normal View History

2022-12-13 23:50:49 +08:00
// File: time_complexity_test.go
2022-12-13 09:53:17 +08:00
// Created Time: 2022-12-13
// Author: msk397 (machangxinq@gmail.com)
package chapter_computational_complexity
2022-12-13 09:24:59 +08:00
import (
"fmt"
"testing"
)
func TestTimeComplexity(t *testing.T) {
n := 8
fmt.Println("输入数据大小 n =", n)
count := constant(n)
fmt.Println("常数阶的操作数量 =", count)
2022-12-13 09:24:59 +08:00
count = linear(n)
fmt.Println("线性阶的操作数量 =", count)
2022-12-13 09:24:59 +08:00
count = arrayTraversal(make([]int, n))
fmt.Println("线性阶(遍历数组)的操作数量 =", count)
2022-12-13 09:24:59 +08:00
count = quadratic(n)
fmt.Println("平方阶的操作数量 =", count)
2022-12-13 09:24:59 +08:00
nums := make([]int, n)
for i := 0; i < n; i++ {
nums[i] = n - i
}
count = bubbleSort(nums)
fmt.Println("平方阶(冒泡排序)的操作数量 =", count)
2022-12-13 09:24:59 +08:00
count = exponential(n)
fmt.Println("指数阶(循环实现)的操作数量 =", count)
2022-12-13 09:24:59 +08:00
count = expRecur(n)
fmt.Println("指数阶(递归实现)的操作数量 =", count)
2022-12-13 09:24:59 +08:00
count = logarithmic(float64(n))
fmt.Println("对数阶(循环实现)的操作数量 =", count)
2022-12-13 09:24:59 +08:00
count = logRecur(float64(n))
fmt.Println("对数阶(递归实现)的操作数量 =", count)
2022-12-13 09:24:59 +08:00
count = linearLogRecur(float64(n))
fmt.Println("线性对数阶(递归实现)的操作数量 =", count)
2022-12-13 09:24:59 +08:00
count = factorialRecur(n)
fmt.Println("阶乘阶(递归实现)的操作数量 =", count)
2022-12-13 09:24:59 +08:00
}