Polish the chapter

introduction, computational complexity.
This commit is contained in:
krahets
2023-08-20 14:51:39 +08:00
parent 5fb728b3d6
commit 2626de8d0b
87 changed files with 375 additions and 371 deletions

View File

@@ -132,37 +132,37 @@ int main() {
cout << "输入数据大小 n = " << n << endl;
int count = constant(n);
cout << "常数阶的计算操作数量 = " << count << endl;
cout << "常数阶的操作数量 = " << count << endl;
count = linear(n);
cout << "线性阶的计算操作数量 = " << count << endl;
cout << "线性阶的操作数量 = " << count << endl;
vector<int> arr(n);
count = arrayTraversal(arr);
cout << "线性阶(遍历数组)的计算操作数量 = " << count << endl;
cout << "线性阶(遍历数组)的操作数量 = " << count << endl;
count = quadratic(n);
cout << "平方阶的计算操作数量 = " << count << endl;
cout << "平方阶的操作数量 = " << count << endl;
vector<int> nums(n);
for (int i = 0; i < n; i++)
nums[i] = n - i; // [n,n-1,...,2,1]
count = bubbleSort(nums);
cout << "平方阶(冒泡排序)的计算操作数量 = " << count << endl;
cout << "平方阶(冒泡排序)的操作数量 = " << count << endl;
count = exponential(n);
cout << "指数阶(循环实现)的计算操作数量 = " << count << endl;
cout << "指数阶(循环实现)的操作数量 = " << count << endl;
count = expRecur(n);
cout << "指数阶(递归实现)的计算操作数量 = " << count << endl;
cout << "指数阶(递归实现)的操作数量 = " << count << endl;
count = logarithmic((float)n);
cout << "对数阶(循环实现)的计算操作数量 = " << count << endl;
cout << "对数阶(循环实现)的操作数量 = " << count << endl;
count = logRecur((float)n);
cout << "对数阶(递归实现)的计算操作数量 = " << count << endl;
cout << "对数阶(递归实现)的操作数量 = " << count << endl;
count = linearLogRecur((float)n);
cout << "线性对数阶(递归实现)的计算操作数量 = " << count << endl;
cout << "线性对数阶(递归实现)的操作数量 = " << count << endl;
count = factorialRecur(n);
cout << "阶乘阶(递归实现)的计算操作数量 = " << count << endl;
cout << "阶乘阶(递归实现)的操作数量 = " << count << endl;
return 0;
}

View File

@@ -9,7 +9,7 @@
/* 大顶堆 */
class MaxHeap {
private:
// 使用动态数组,这样无考虑扩容问题
// 使用动态数组,这样无考虑扩容问题
vector<int> maxHeap;
/* 获取左子节点索引 */
@@ -32,7 +32,7 @@ class MaxHeap {
while (true) {
// 获取节点 i 的父节点
int p = parent(i);
// 当“越过根节点”或“节点无修复”时,结束堆化
// 当“越过根节点”或“节点无修复”时,结束堆化
if (p < 0 || maxHeap[i] <= maxHeap[p])
break;
// 交换两节点
@@ -47,12 +47,12 @@ class MaxHeap {
while (true) {
// 判断节点 i, l, r 中值最大的节点,记为 ma
int l = left(i), r = right(i), ma = i;
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
if (l < size() && maxHeap[l] > maxHeap[ma])
ma = l;
if (r < size() && maxHeap[r] > maxHeap[ma])
ma = r;
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
if (ma == i)
break;
swap(maxHeap[i], maxHeap[ma]);

View File

@@ -17,7 +17,7 @@ void siftDown(vector<int> &nums, int n, int i) {
ma = l;
if (r < n && nums[r] > nums[ma])
ma = r;
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
if (ma == i) {
break;
}

View File

@@ -71,7 +71,7 @@ class AVLTree {
return leftRotate(node);
}
}
// 平衡树,无旋转,直接返回
// 平衡树,无旋转,直接返回
return node;
}