mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-27 17:10:28 +08:00
Polish the chapter
introduction, computational complexity.
This commit is contained in:
@@ -14,35 +14,35 @@ func TestTimeComplexity(t *testing.T) {
|
||||
fmt.Println("输入数据大小 n =", n)
|
||||
|
||||
count := constant(n)
|
||||
fmt.Println("常数阶的计算操作数量 =", count)
|
||||
fmt.Println("常数阶的操作数量 =", count)
|
||||
|
||||
count = linear(n)
|
||||
fmt.Println("线性阶的计算操作数量 =", count)
|
||||
fmt.Println("线性阶的操作数量 =", count)
|
||||
count = arrayTraversal(make([]int, n))
|
||||
fmt.Println("线性阶(遍历数组)的计算操作数量 =", count)
|
||||
fmt.Println("线性阶(遍历数组)的操作数量 =", count)
|
||||
|
||||
count = quadratic(n)
|
||||
fmt.Println("平方阶的计算操作数量 =", count)
|
||||
fmt.Println("平方阶的操作数量 =", count)
|
||||
nums := make([]int, n)
|
||||
for i := 0; i < n; i++ {
|
||||
nums[i] = n - i
|
||||
}
|
||||
count = bubbleSort(nums)
|
||||
fmt.Println("平方阶(冒泡排序)的计算操作数量 =", count)
|
||||
fmt.Println("平方阶(冒泡排序)的操作数量 =", count)
|
||||
|
||||
count = exponential(n)
|
||||
fmt.Println("指数阶(循环实现)的计算操作数量 =", count)
|
||||
fmt.Println("指数阶(循环实现)的操作数量 =", count)
|
||||
count = expRecur(n)
|
||||
fmt.Println("指数阶(递归实现)的计算操作数量 =", count)
|
||||
fmt.Println("指数阶(递归实现)的操作数量 =", count)
|
||||
|
||||
count = logarithmic(float64(n))
|
||||
fmt.Println("对数阶(循环实现)的计算操作数量 =", count)
|
||||
fmt.Println("对数阶(循环实现)的操作数量 =", count)
|
||||
count = logRecur(float64(n))
|
||||
fmt.Println("对数阶(递归实现)的计算操作数量 =", count)
|
||||
fmt.Println("对数阶(递归实现)的操作数量 =", count)
|
||||
|
||||
count = linearLogRecur(float64(n))
|
||||
fmt.Println("线性对数阶(递归实现)的计算操作数量 =", count)
|
||||
fmt.Println("线性对数阶(递归实现)的操作数量 =", count)
|
||||
|
||||
count = factorialRecur(n)
|
||||
fmt.Println("阶乘阶(递归实现)的计算操作数量 =", count)
|
||||
fmt.Println("阶乘阶(递归实现)的操作数量 =", count)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
type maxHeap struct {
|
||||
// 使用切片而非数组,这样无需考虑扩容问题
|
||||
// 使用切片而非数组,这样无须考虑扩容问题
|
||||
data []any
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ func (h *maxHeap) siftUp(i int) {
|
||||
for true {
|
||||
// 获取节点 i 的父节点
|
||||
p := h.parent(i)
|
||||
// 当“越过根节点”或“节点无需修复”时,结束堆化
|
||||
// 当“越过根节点”或“节点无须修复”时,结束堆化
|
||||
if p < 0 || h.data[i].(int) <= h.data[p].(int) {
|
||||
break
|
||||
}
|
||||
@@ -123,7 +123,7 @@ func (h *maxHeap) siftDown(i int) {
|
||||
if r < h.size() && h.data[r].(int) > h.data[max].(int) {
|
||||
max = r
|
||||
}
|
||||
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
||||
// 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出
|
||||
if max == i {
|
||||
break
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ func siftDown(nums *[]int, n, i int) {
|
||||
if r < n && (*nums)[r] > (*nums)[ma] {
|
||||
ma = r
|
||||
}
|
||||
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
||||
// 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出
|
||||
if ma == i {
|
||||
break
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ func (t *aVLTree) rotate(node *TreeNode) *TreeNode {
|
||||
return t.leftRotate(node)
|
||||
}
|
||||
}
|
||||
// 平衡树,无需旋转,直接返回
|
||||
// 平衡树,无须旋转,直接返回
|
||||
return node
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user