mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-13 12:20:57 +00:00
Polish the chapter
introduction, computational complexity.
This commit is contained in:
@@ -128,36 +128,36 @@ void main() {
|
||||
print('输入数据大小 n = $n');
|
||||
|
||||
int count = constant(n);
|
||||
print('常数阶的计算操作数量 = $count');
|
||||
print('常数阶的操作数量 = $count');
|
||||
|
||||
count = linear(n);
|
||||
print('线性阶的计算操作数量 = $count');
|
||||
print('线性阶的操作数量 = $count');
|
||||
|
||||
count = arrayTraversal(List.filled(n, 0));
|
||||
print('线性阶(遍历数组)的计算操作数量 = $count');
|
||||
print('线性阶(遍历数组)的操作数量 = $count');
|
||||
|
||||
count = quadratic(n);
|
||||
print('平方阶的计算操作数量 = $count');
|
||||
print('平方阶的操作数量 = $count');
|
||||
final nums = List.filled(n, 0);
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
}
|
||||
count = bubbleSort(nums);
|
||||
print('平方阶(冒泡排序)的计算操作数量 = $count');
|
||||
print('平方阶(冒泡排序)的操作数量 = $count');
|
||||
|
||||
count = exponential(n);
|
||||
print('指数阶(循环实现)的计算操作数量 = $count');
|
||||
print('指数阶(循环实现)的操作数量 = $count');
|
||||
count = expRecur(n);
|
||||
print('指数阶(递归实现)的计算操作数量 = $count');
|
||||
print('指数阶(递归实现)的操作数量 = $count');
|
||||
|
||||
count = logarithmic(n);
|
||||
print('对数阶(循环实现)的计算操作数量 = $count');
|
||||
print('对数阶(循环实现)的操作数量 = $count');
|
||||
count = logRecur(n);
|
||||
print('对数阶(递归实现)的计算操作数量 = $count');
|
||||
print('对数阶(递归实现)的操作数量 = $count');
|
||||
|
||||
count = linearLogRecur(n);
|
||||
print('线性对数阶(递归实现)的计算操作数量 = $count');
|
||||
print('线性对数阶(递归实现)的操作数量 = $count');
|
||||
|
||||
count = factorialRecur(n);
|
||||
print('阶乘阶(递归实现)的计算操作数量 = $count');
|
||||
print('阶乘阶(递归实现)的操作数量 = $count');
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ class MaxHeap {
|
||||
while (true) {
|
||||
// 获取节点 i 的父节点
|
||||
int p = _parent(i);
|
||||
// 当“越过根节点”或“节点无需修复”时,结束堆化
|
||||
// 当“越过根节点”或“节点无须修复”时,结束堆化
|
||||
if (p < 0 || _maxHeap[i] <= _maxHeap[p]) {
|
||||
break;
|
||||
}
|
||||
@@ -104,7 +104,7 @@ class MaxHeap {
|
||||
int ma = i;
|
||||
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(i, ma);
|
||||
|
||||
@@ -100,7 +100,7 @@ class MinHeap {
|
||||
while (true) {
|
||||
// 获取节点 i 的父节点
|
||||
int p = _parent(i);
|
||||
// 当“越过根节点”或“节点无需修复”时,结束堆化
|
||||
// 当“越过根节点”或“节点无须修复”时,结束堆化
|
||||
if (p < 0 || _minHeap[i] >= _minHeap[p]) {
|
||||
break;
|
||||
}
|
||||
@@ -134,7 +134,7 @@ class MinHeap {
|
||||
int mi = i;
|
||||
if (l < size() && _minHeap[l] < _minHeap[mi]) mi = l;
|
||||
if (r < size() && _minHeap[r] < _minHeap[mi]) mi = r;
|
||||
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
||||
// 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出
|
||||
if (mi == i) break;
|
||||
// 交换两节点
|
||||
_swap(i, mi);
|
||||
|
||||
@@ -13,7 +13,7 @@ void siftDown(List<int> nums, int n, int i) {
|
||||
int ma = i;
|
||||
if (l < n && nums[l] > nums[ma]) ma = l;
|
||||
if (r < n && nums[r] > nums[ma]) ma = r;
|
||||
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
||||
// 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出
|
||||
if (ma == i) break;
|
||||
// 交换两节点
|
||||
int temp = nums[i];
|
||||
|
||||
@@ -90,7 +90,7 @@ class AVLTree {
|
||||
return leftRotate(node);
|
||||
}
|
||||
}
|
||||
// 平衡树,无需旋转,直接返回
|
||||
// 平衡树,无须旋转,直接返回
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user