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
@@ -135,38 +135,38 @@ int main(int argc, char *argv[]) {
printf("输入数据大小 n = %d\n", n);
int count = constant(n);
printf("常数阶的计算操作数量 = %d\n", count);
printf("常数阶的操作数量 = %d\n", count);
count = linear(n);
printf("线性阶的计算操作数量 = %d\n", count);
printf("线性阶的操作数量 = %d\n", count);
// 分配堆区内存(创建一维可变长数组:数组中元素数量为n,元素类型为int)
int *nums = (int *)malloc(n * sizeof(int));
count = arrayTraversal(nums, n);
printf("线性阶(遍历数组)的计算操作数量 = %d\n", count);
printf("线性阶(遍历数组)的操作数量 = %d\n", count);
count = quadratic(n);
printf("平方阶的计算操作数量 = %d\n", count);
printf("平方阶的操作数量 = %d\n", count);
for (int i = 0; i < n; i++) {
nums[i] = n - i; // [n,n-1,...,2,1]
}
count = bubbleSort(nums, n);
printf("平方阶(冒泡排序)的计算操作数量 = %d\n", count);
printf("平方阶(冒泡排序)的操作数量 = %d\n", count);
count = exponential(n);
printf("指数阶(循环实现)的计算操作数量 = %d\n", count);
printf("指数阶(循环实现)的操作数量 = %d\n", count);
count = expRecur(n);
printf("指数阶(递归实现)的计算操作数量 = %d\n", count);
printf("指数阶(递归实现)的操作数量 = %d\n", count);
count = logarithmic(n);
printf("对数阶(循环实现)的计算操作数量 = %d\n", count);
printf("对数阶(循环实现)的操作数量 = %d\n", count);
count = logRecur(n);
printf("对数阶(递归实现)的计算操作数量 = %d\n", count);
printf("对数阶(递归实现)的操作数量 = %d\n", count);
count = linearLogRecur(n);
printf("线性对数阶(递归实现)的计算操作数量 = %d\n", count);
printf("线性对数阶(递归实现)的操作数量 = %d\n", count);
count = factorialRecur(n);
printf("阶乘阶(递归实现)的计算操作数量 = %d\n", count);
printf("阶乘阶(递归实现)的操作数量 = %d\n", count);
// 释放堆区内存
if (nums != NULL) {
+2 -2
View File
@@ -119,7 +119,7 @@ void siftDown(maxHeap *h, int i) {
if (r < size(h) && h->data[r] > h->data[max]) {
max = r;
}
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
if (max == i) {
break;
}
@@ -135,7 +135,7 @@ void siftUp(maxHeap *h, int i) {
while (true) {
// 获取节点 i 的父节点
int p = parent(h, i);
// 当“越过根节点”或“节点无修复”时,结束堆化
// 当“越过根节点”或“节点无修复”时,结束堆化
if (p < 0 || h->data[i] <= h->data[p]) {
break;
}
+1 -1
View File
@@ -17,7 +17,7 @@ void siftDown(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;
}
+1 -1
View File
@@ -107,7 +107,7 @@ TreeNode *rotate(TreeNode *node) {
return leftRotate(node);
}
}
// 平衡树,无旋转,直接返回
// 平衡树,无旋转,直接返回
return node;
}
@@ -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;
}
+4 -4
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]);
+1 -1
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;
}
+1 -1
View File
@@ -71,7 +71,7 @@ class AVLTree {
return leftRotate(node);
}
}
// 平衡树,无旋转,直接返回
// 平衡树,无旋转,直接返回
return node;
}
@@ -160,35 +160,35 @@ public class time_complexity {
Console.WriteLine("输入数据大小 n = " + n);
int count = constant(n);
Console.WriteLine("常数阶的计算操作数量 = " + count);
Console.WriteLine("常数阶的操作数量 = " + count);
count = linear(n);
Console.WriteLine("线性阶的计算操作数量 = " + count);
Console.WriteLine("线性阶的操作数量 = " + count);
count = arrayTraversal(new int[n]);
Console.WriteLine("线性阶(遍历数组)的计算操作数量 = " + count);
Console.WriteLine("线性阶(遍历数组)的操作数量 = " + count);
count = quadratic(n);
Console.WriteLine("平方阶的计算操作数量 = " + count);
Console.WriteLine("平方阶的操作数量 = " + count);
int[] nums = new int[n];
for (int i = 0; i < n; i++)
nums[i] = n - i; // [n,n-1,...,2,1]
count = bubbleSort(nums);
Console.WriteLine("平方阶(冒泡排序)的计算操作数量 = " + count);
Console.WriteLine("平方阶(冒泡排序)的操作数量 = " + count);
count = exponential(n);
Console.WriteLine("指数阶(循环实现)的计算操作数量 = " + count);
Console.WriteLine("指数阶(循环实现)的操作数量 = " + count);
count = expRecur(n);
Console.WriteLine("指数阶(递归实现)的计算操作数量 = " + count);
Console.WriteLine("指数阶(递归实现)的操作数量 = " + count);
count = logarithmic((float)n);
Console.WriteLine("对数阶(循环实现)的计算操作数量 = " + count);
Console.WriteLine("对数阶(循环实现)的操作数量 = " + count);
count = logRecur((float)n);
Console.WriteLine("对数阶(递归实现)的计算操作数量 = " + count);
Console.WriteLine("对数阶(递归实现)的操作数量 = " + count);
count = linearLogRecur((float)n);
Console.WriteLine("线性对数阶(递归实现)的计算操作数量 = " + count);
Console.WriteLine("线性对数阶(递归实现)的操作数量 = " + count);
count = factorialRecur(n);
Console.WriteLine("阶乘阶(递归实现)的计算操作数量 = " + count);
Console.WriteLine("阶乘阶(递归实现)的操作数量 = " + count);
}
}
+2 -2
View File
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_heap;
/* 大顶堆 */
class MaxHeap {
// 使用列表而非数组,这样无考虑扩容问题
// 使用列表而非数组,这样无考虑扩容问题
private readonly List<int> maxHeap;
/* 构造函数,建立空堆 */
@@ -70,7 +70,7 @@ class MaxHeap {
while (true) {
// 获取节点 i 的父节点
int p = parent(i);
// 若“越过根节点”或“节点无修复”,则结束堆化
// 若“越过根节点”或“节点无修复”,则结束堆化
if (p < 0 || maxHeap[i] <= maxHeap[p])
break;
// 交换两节点
+1 -1
View File
@@ -18,7 +18,7 @@ public class heap_sort {
ma = l;
if (r < n && nums[r] > nums[ma])
ma = r;
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
if (ma == i)
break;
// 交换两节点
+1 -1
View File
@@ -84,7 +84,7 @@ class AVLTree {
return leftRotate(node);
}
}
// 平衡树,无旋转,直接返回
// 平衡树,无旋转,直接返回
return node;
}
@@ -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');
}
+2 -2
View File
@@ -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);
+2 -2
View File
@@ -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);
+1 -1
View File
@@ -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];
+1 -1
View File
@@ -90,7 +90,7 @@ class AVLTree {
return leftRotate(node);
}
}
// 平衡树,无旋转,直接返回
// 平衡树,无旋转,直接返回
return node;
}
@@ -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)
}
+3 -3
View File
@@ -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
}
+1 -1
View File
@@ -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
}
+1 -1
View File
@@ -102,7 +102,7 @@ func (t *aVLTree) rotate(node *TreeNode) *TreeNode {
return t.leftRotate(node)
}
}
// 平衡树,无旋转,直接返回
// 平衡树,无旋转,直接返回
return node
}
@@ -134,35 +134,35 @@ public class time_complexity {
System.out.println("输入数据大小 n = " + n);
int count = constant(n);
System.out.println("常数阶的计算操作数量 = " + count);
System.out.println("常数阶的操作数量 = " + count);
count = linear(n);
System.out.println("线性阶的计算操作数量 = " + count);
System.out.println("线性阶的操作数量 = " + count);
count = arrayTraversal(new int[n]);
System.out.println("线性阶(遍历数组)的计算操作数量 = " + count);
System.out.println("线性阶(遍历数组)的操作数量 = " + count);
count = quadratic(n);
System.out.println("平方阶的计算操作数量 = " + count);
System.out.println("平方阶的操作数量 = " + count);
int[] nums = new int[n];
for (int i = 0; i < n; i++)
nums[i] = n - i; // [n,n-1,...,2,1]
count = bubbleSort(nums);
System.out.println("平方阶(冒泡排序)的计算操作数量 = " + count);
System.out.println("平方阶(冒泡排序)的操作数量 = " + count);
count = exponential(n);
System.out.println("指数阶(循环实现)的计算操作数量 = " + count);
System.out.println("指数阶(循环实现)的操作数量 = " + count);
count = expRecur(n);
System.out.println("指数阶(递归实现)的计算操作数量 = " + count);
System.out.println("指数阶(递归实现)的操作数量 = " + count);
count = logarithmic((float) n);
System.out.println("对数阶(循环实现)的计算操作数量 = " + count);
System.out.println("对数阶(循环实现)的操作数量 = " + count);
count = logRecur((float) n);
System.out.println("对数阶(递归实现)的计算操作数量 = " + count);
System.out.println("对数阶(递归实现)的操作数量 = " + count);
count = linearLogRecur((float) n);
System.out.println("线性对数阶(递归实现)的计算操作数量 = " + count);
System.out.println("线性对数阶(递归实现)的操作数量 = " + count);
count = factorialRecur(n);
System.out.println("阶乘阶(递归实现)的计算操作数量 = " + count);
System.out.println("阶乘阶(递归实现)的操作数量 = " + count);
}
}
+3 -3
View File
@@ -11,7 +11,7 @@ import java.util.*;
/* 大顶堆 */
class MaxHeap {
// 使用列表而非数组,这样无考虑扩容问题
// 使用列表而非数组,这样无考虑扩容问题
private List<Integer> maxHeap;
/* 构造方法,根据输入列表建堆 */
@@ -74,7 +74,7 @@ class MaxHeap {
while (true) {
// 获取节点 i 的父节点
int p = parent(i);
// 当“越过根节点”或“节点无修复”时,结束堆化
// 当“越过根节点”或“节点无修复”时,结束堆化
if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))
break;
// 交换两节点
@@ -108,7 +108,7 @@ class MaxHeap {
ma = l;
if (r < size() && maxHeap.get(r) > maxHeap.get(ma))
ma = r;
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
if (ma == i)
break;
// 交换两节点
+1 -1
View File
@@ -20,7 +20,7 @@ public class heap_sort {
ma = l;
if (r < n && nums[r] > nums[ma])
ma = r;
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
if (ma == i)
break;
// 交换两节点
+1 -1
View File
@@ -87,7 +87,7 @@ class AVLTree {
return leftRotate(node);
}
}
// 平衡树,无旋转,直接返回
// 平衡树,无旋转,直接返回
return node;
}
@@ -124,32 +124,32 @@ const n = 8;
console.log('输入数据大小 n = ' + n);
let count = constant(n);
console.log('常数阶的计算操作数量 = ' + count);
console.log('常数阶的操作数量 = ' + count);
count = linear(n);
console.log('线性阶的计算操作数量 = ' + count);
console.log('线性阶的操作数量 = ' + count);
count = arrayTraversal(new Array(n));
console.log('线性阶(遍历数组)的计算操作数量 = ' + count);
console.log('线性阶(遍历数组)的操作数量 = ' + count);
count = quadratic(n);
console.log('平方阶的计算操作数量 = ' + count);
console.log('平方阶的操作数量 = ' + count);
let nums = new Array(n);
for (let i = 0; i < n; i++) nums[i] = n - i; // [n,n-1,...,2,1]
count = bubbleSort(nums);
console.log('平方阶(冒泡排序)的计算操作数量 = ' + count);
console.log('平方阶(冒泡排序)的操作数量 = ' + count);
count = exponential(n);
console.log('指数阶(循环实现)的计算操作数量 = ' + count);
console.log('指数阶(循环实现)的操作数量 = ' + count);
count = expRecur(n);
console.log('指数阶(递归实现)的计算操作数量 = ' + count);
console.log('指数阶(递归实现)的操作数量 = ' + count);
count = logarithmic(n);
console.log('对数阶(循环实现)的计算操作数量 = ' + count);
console.log('对数阶(循环实现)的操作数量 = ' + count);
count = logRecur(n);
console.log('对数阶(递归实现)的计算操作数量 = ' + count);
console.log('对数阶(递归实现)的操作数量 = ' + count);
count = linearLogRecur(n);
console.log('线性对数阶(递归实现)的计算操作数量 = ' + count);
console.log('线性对数阶(递归实现)的操作数量 = ' + count);
count = factorialRecur(n);
console.log('阶乘阶(递归实现)的计算操作数量 = ' + count);
console.log('阶乘阶(递归实现)的操作数量 = ' + count);
+2 -2
View File
@@ -70,7 +70,7 @@ class MaxHeap {
while (true) {
// 获取节点 i 的父节点
const p = this.#parent(i);
// 当“越过根节点”或“节点无修复”时,结束堆化
// 当“越过根节点”或“节点无修复”时,结束堆化
if (p < 0 || this.#maxHeap[i] <= this.#maxHeap[p]) break;
// 交换两节点
this.#swap(i, p);
@@ -102,7 +102,7 @@ class MaxHeap {
let ma = i;
if (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l;
if (r < this.size() && this.#maxHeap[r] > this.#maxHeap[ma]) ma = r;
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
if (ma === i) break;
// 交换两节点
this.#swap(i, ma);
@@ -17,7 +17,7 @@ function siftDown(nums, n, i) {
if (r < n && nums[r] > nums[ma]) {
ma = r;
}
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
if (ma === i) {
break;
}
+1 -1
View File
@@ -89,7 +89,7 @@ class AVLTree {
return this.#leftRotate(node);
}
}
// 平衡树,无旋转,直接返回
// 平衡树,无旋转,直接返回
return node;
}
@@ -121,31 +121,31 @@ if __name__ == "__main__":
print("输入数据大小 n =", n)
count: int = constant(n)
print("常数阶的计算操作数量 =", count)
print("常数阶的操作数量 =", count)
count: int = linear(n)
print("线性阶的计算操作数量 =", count)
print("线性阶的操作数量 =", count)
count: int = array_traversal([0] * n)
print("线性阶(遍历数组)的计算操作数量 =", count)
print("线性阶(遍历数组)的操作数量 =", count)
count: int = quadratic(n)
print("平方阶的计算操作数量 =", count)
print("平方阶的操作数量 =", count)
nums = [i for i in range(n, 0, -1)] # [n, n-1, ..., 2, 1]
count: int = bubble_sort(nums)
print("平方阶(冒泡排序)的计算操作数量 =", count)
print("平方阶(冒泡排序)的操作数量 =", count)
count: int = exponential(n)
print("指数阶(循环实现)的计算操作数量 =", count)
print("指数阶(循环实现)的操作数量 =", count)
count: int = exp_recur(n)
print("指数阶(递归实现)的计算操作数量 =", count)
print("指数阶(递归实现)的操作数量 =", count)
count: int = logarithmic(n)
print("对数阶(循环实现)的计算操作数量 =", count)
print("对数阶(循环实现)的操作数量 =", count)
count: int = log_recur(n)
print("对数阶(递归实现)的计算操作数量 =", count)
print("对数阶(递归实现)的操作数量 =", count)
count: int = linear_log_recur(n)
print("线性对数阶(递归实现)的计算操作数量 =", count)
print("线性对数阶(递归实现)的操作数量 =", count)
count: int = factorial_recur(n)
print("阶乘阶(递归实现)的计算操作数量 =", count)
print("阶乘阶(递归实现)的操作数量 =", count)
+2 -2
View File
@@ -61,7 +61,7 @@ class MaxHeap:
while True:
# 获取节点 i 的父节点
p = self.parent(i)
# 当“越过根节点”或“节点无修复”时,结束堆化
# 当“越过根节点”或“节点无修复”时,结束堆化
if p < 0 or self.max_heap[i] <= self.max_heap[p]:
break
# 交换两节点
@@ -92,7 +92,7 @@ class MaxHeap:
ma = l
if r < self.size() and self.max_heap[r] > self.max_heap[ma]:
ma = r
# 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
# 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
if ma == i:
break
# 交换两节点
@@ -11,7 +11,7 @@ def binary_search(nums: list[int], target: int) -> int:
i, j = 0, len(nums) - 1
# 循环,当搜索区间为空时跳出(当 i > j 时为空)
while i <= j:
# 理论上 Python 的数字可以无限大(取决于内存大小),无考虑大数越界问题
# 理论上 Python 的数字可以无限大(取决于内存大小),无考虑大数越界问题
m = (i + j) // 2 # 计算中点索引 m
if nums[m] < target:
i = m + 1 # 此情况说明 target 在区间 [m+1, j] 中
+1 -1
View File
@@ -16,7 +16,7 @@ def sift_down(nums: list[int], n: int, i: int):
ma = l
if r < n and nums[r] > nums[ma]:
ma = r
# 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
# 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
if ma == i:
break
# 交换两节点
+1 -1
View File
@@ -85,7 +85,7 @@ class AVLTree:
# 先右旋后左旋
node.right = self.__right_rotate(node.right)
return self.__left_rotate(node)
# 平衡树,无旋转,直接返回
# 平衡树,无旋转,直接返回
return node
def insert(self, val):
@@ -139,32 +139,32 @@ fn main() {
println!("输入数据大小 n = {}", n);
let mut count = constant(n);
println!("常数阶的计算操作数量 = {}", count);
println!("常数阶的操作数量 = {}", count);
count = linear(n);
println!("线性阶的计算操作数量 = {}", count);
println!("线性阶的操作数量 = {}", count);
count = array_traversal(&vec![0; n as usize]);
println!("线性阶(遍历数组)的计算操作数量 = {}", count);
println!("线性阶(遍历数组)的操作数量 = {}", count);
count = quadratic(n);
println!("平方阶的计算操作数量 = {}", count);
println!("平方阶的操作数量 = {}", count);
let mut nums = (1..=n).rev().collect::<Vec<_>>(); // [n,n-1,...,2,1]
count = bubble_sort(&mut nums);
println!("平方阶(冒泡排序)的计算操作数量 = {}", count);
println!("平方阶(冒泡排序)的操作数量 = {}", count);
count = exponential(n);
println!("指数阶(循环实现)的计算操作数量 = {}", count);
println!("指数阶(循环实现)的操作数量 = {}", count);
count = exp_recur(n);
println!("指数阶(递归实现)的计算操作数量 = {}", count);
println!("指数阶(递归实现)的操作数量 = {}", count);
count = logarithmic(n as f32);
println!("对数阶(循环实现)的计算操作数量 = {}", count);
println!("对数阶(循环实现)的操作数量 = {}", count);
count = log_recur(n as f32);
println!("对数阶(递归实现)的计算操作数量 = {}", count);
println!("对数阶(递归实现)的操作数量 = {}", count);
count = linear_log_recur(n as f32);
println!("线性对数阶(递归实现)的计算操作数量 = {}", count);
println!("线性对数阶(递归实现)的操作数量 = {}", count);
count = factorial_recur(n);
println!("阶乘阶(递归实现)的计算操作数量 = {}", count);
println!("阶乘阶(递归实现)的操作数量 = {}", count);
}
+3 -3
View File
@@ -8,7 +8,7 @@ include!("../include/include.rs");
/* 大顶堆 */
struct MaxHeap {
// 使用 vector 而非数组,这样无考虑扩容问题
// 使用 vector 而非数组,这样无考虑扩容问题
max_heap: Vec<i32>,
}
@@ -76,7 +76,7 @@ impl MaxHeap {
}
// 获取节点 i 的父节点
let p = Self::parent(i);
// 当“节点无修复”时,结束堆化
// 当“节点无修复”时,结束堆化
if self.max_heap[i] <= self.max_heap[p] {
break;
}
@@ -114,7 +114,7 @@ impl MaxHeap {
if r < self.size() && self.max_heap[r] > self.max_heap[ma] {
ma = r;
}
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
if ma == i {
break;
}
+1 -1
View File
@@ -19,7 +19,7 @@ fn sift_down(nums: &mut [i32], n: usize, mut i: usize) {
if r < n && nums[r] > nums[ma] {
ma = r;
}
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
if ma == i {
break;
}
+1 -1
View File
@@ -123,7 +123,7 @@ impl AVLTree {
Self::left_rotate(Some(node))
}
} else {
// 平衡树,无旋转,直接返回
// 平衡树,无旋转,直接返回
node
}
}
@@ -140,33 +140,33 @@ enum TimeComplexity {
print("输入数据大小 n = \(n)")
var count = constant(n: n)
print("常数阶的计算操作数量 = \(count)")
print("常数阶的操作数量 = \(count)")
count = linear(n: n)
print("线性阶的计算操作数量 = \(count)")
print("线性阶的操作数量 = \(count)")
count = arrayTraversal(nums: Array(repeating: 0, count: n))
print("线性阶(遍历数组)的计算操作数量 = \(count)")
print("线性阶(遍历数组)的操作数量 = \(count)")
count = quadratic(n: n)
print("平方阶的计算操作数量 = \(count)")
print("平方阶的操作数量 = \(count)")
var nums = Array(stride(from: n, to: 0, by: -1)) // [n,n-1,...,2,1]
count = bubbleSort(nums: &nums)
print("平方阶(冒泡排序)的计算操作数量 = \(count)")
print("平方阶(冒泡排序)的操作数量 = \(count)")
count = exponential(n: n)
print("指数阶(循环实现)的计算操作数量 = \(count)")
print("指数阶(循环实现)的操作数量 = \(count)")
count = expRecur(n: n)
print("指数阶(递归实现)的计算操作数量 = \(count)")
print("指数阶(递归实现)的操作数量 = \(count)")
count = logarithmic(n: Double(n))
print("对数阶(循环实现)的计算操作数量 = \(count)")
print("对数阶(循环实现)的操作数量 = \(count)")
count = logRecur(n: Double(n))
print("对数阶(递归实现)的计算操作数量 = \(count)")
print("对数阶(递归实现)的操作数量 = \(count)")
count = linearLogRecur(n: Double(n))
print("线性对数阶(递归实现)的计算操作数量 = \(count)")
print("线性对数阶(递归实现)的操作数量 = \(count)")
count = factorialRecur(n: n)
print("阶乘阶(递归实现)的计算操作数量 = \(count)")
print("阶乘阶(递归实现)的操作数量 = \(count)")
}
}
+2 -2
View File
@@ -69,7 +69,7 @@ class MaxHeap {
while true {
// i
let p = parent(i: i)
//
//
if p < 0 || maxHeap[i] <= maxHeap[p] {
break
}
@@ -110,7 +110,7 @@ class MaxHeap {
if r < size(), maxHeap[r] > maxHeap[ma] {
ma = r
}
// i l, r
// i l, r
if ma == i {
break
}
+1 -1
View File
@@ -18,7 +18,7 @@ func siftDown(nums: inout [Int], n: Int, i: Int) {
if r < n, nums[r] > nums[ma] {
ma = r
}
// i l, r
// i l, r
if ma == i {
break
}
+1 -1
View File
@@ -84,7 +84,7 @@ class AVLTree {
return leftRotate(node: node)
}
}
//
//
return node
}
@@ -124,34 +124,34 @@ const n = 8;
console.log('输入数据大小 n = ' + n);
let count = constant(n);
console.log('常数阶的计算操作数量 = ' + count);
console.log('常数阶的操作数量 = ' + count);
count = linear(n);
console.log('线性阶的计算操作数量 = ' + count);
console.log('线性阶的操作数量 = ' + count);
count = arrayTraversal(new Array(n));
console.log('线性阶(遍历数组)的计算操作数量 = ' + count);
console.log('线性阶(遍历数组)的操作数量 = ' + count);
count = quadratic(n);
console.log('平方阶的计算操作数量 = ' + count);
console.log('平方阶的操作数量 = ' + count);
var nums = new Array(n);
for (let i = 0; i < n; i++) nums[i] = n - i; // [n,n-1,...,2,1]
count = bubbleSort(nums);
console.log('平方阶(冒泡排序)的计算操作数量 = ' + count);
console.log('平方阶(冒泡排序)的操作数量 = ' + count);
count = exponential(n);
console.log('指数阶(循环实现)的计算操作数量 = ' + count);
console.log('指数阶(循环实现)的操作数量 = ' + count);
count = expRecur(n);
console.log('指数阶(递归实现)的计算操作数量 = ' + count);
console.log('指数阶(递归实现)的操作数量 = ' + count);
count = logarithmic(n);
console.log('对数阶(循环实现)的计算操作数量 = ' + count);
console.log('对数阶(循环实现)的操作数量 = ' + count);
count = logRecur(n);
console.log('对数阶(递归实现)的计算操作数量 = ' + count);
console.log('对数阶(递归实现)的操作数量 = ' + count);
count = linearLogRecur(n);
console.log('线性对数阶(递归实现)的计算操作数量 = ' + count);
console.log('线性对数阶(递归实现)的操作数量 = ' + count);
count = factorialRecur(n);
console.log('阶乘阶(递归实现)的计算操作数量 = ' + count);
console.log('阶乘阶(递归实现)的操作数量 = ' + count);
export {};
+2 -2
View File
@@ -69,7 +69,7 @@ class MaxHeap {
while (true) {
// 获取节点 i 的父节点
const p = this.parent(i);
// 当“越过根节点”或“节点无修复”时,结束堆化
// 当“越过根节点”或“节点无修复”时,结束堆化
if (p < 0 || this.maxHeap[i] <= this.maxHeap[p]) break;
// 交换两节点
this.swap(i, p);
@@ -101,7 +101,7 @@ class MaxHeap {
let ma = i;
if (l < this.size() && this.maxHeap[l] > this.maxHeap[ma]) ma = l;
if (r < this.size() && this.maxHeap[r] > this.maxHeap[ma]) ma = r;
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
if (ma === i) break;
// 交换两节点
this.swap(i, ma);
@@ -17,7 +17,7 @@ function siftDown(nums: number[], n: number, i: number): void {
if (r < n && nums[r] > nums[ma]) {
ma = r;
}
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
if (ma === i) {
break;
}
+1 -1
View File
@@ -90,7 +90,7 @@ class AVLTree {
return this.leftRotate(node);
}
}
// 平衡树,无旋转,直接返回
// 平衡树,无旋转,直接返回
return node;
}
@@ -143,37 +143,37 @@ pub fn main() !void {
std.debug.print("输入数据大小 n = {}\n", .{n});
var count = constant(n);
std.debug.print("常数阶的计算操作数量 = {}\n", .{count});
std.debug.print("常数阶的操作数量 = {}\n", .{count});
count = linear(n);
std.debug.print("线性阶的计算操作数量 = {}\n", .{count});
std.debug.print("线性阶的操作数量 = {}\n", .{count});
var nums = [_]i32{0}**n;
count = arrayTraversal(&nums);
std.debug.print("线性阶(遍历数组)的计算操作数量 = {}\n", .{count});
std.debug.print("线性阶(遍历数组)的操作数量 = {}\n", .{count});
count = quadratic(n);
std.debug.print("平方阶的计算操作数量 = {}\n", .{count});
std.debug.print("平方阶的操作数量 = {}\n", .{count});
for (&nums, 0..) |*num, i| {
num.* = n - @as(i32, @intCast(i)); // [n,n-1,...,2,1]
}
count = bubbleSort(&nums);
std.debug.print("平方阶(冒泡排序)的计算操作数量 = {}\n", .{count});
std.debug.print("平方阶(冒泡排序)的操作数量 = {}\n", .{count});
count = exponential(n);
std.debug.print("指数阶(循环实现)的计算操作数量 = {}\n", .{count});
std.debug.print("指数阶(循环实现)的操作数量 = {}\n", .{count});
count = expRecur(n);
std.debug.print("指数阶(递归实现)的计算操作数量 = {}\n", .{count});
std.debug.print("指数阶(递归实现)的操作数量 = {}\n", .{count});
count = logarithmic(@as(f32, n));
std.debug.print("对数阶(循环实现)的计算操作数量 = {}\n", .{count});
std.debug.print("对数阶(循环实现)的操作数量 = {}\n", .{count});
count = logRecur(@as(f32, n));
std.debug.print("对数阶(递归实现)的计算操作数量 = {}\n", .{count});
std.debug.print("对数阶(递归实现)的操作数量 = {}\n", .{count});
count = linearLogRecur(@as(f32, n));
std.debug.print("线性对数阶(递归实现)的计算操作数量 = {}\n", .{count});
std.debug.print("线性对数阶(递归实现)的操作数量 = {}\n", .{count});
count = factorialRecur(n);
std.debug.print("阶乘阶(递归实现)的计算操作数量 = {}\n", .{count});
std.debug.print("阶乘阶(递归实现)的操作数量 = {}\n", .{count});
_ = try std.io.getStdIn().reader().readByte();
}
+3 -3
View File
@@ -10,7 +10,7 @@ pub fn MaxHeap(comptime T: type) type {
return struct {
const Self = @This();
max_heap: ?std.ArrayList(T) = null, // 使用列表而非数组,这样无考虑扩容问题
max_heap: ?std.ArrayList(T) = null, // 使用列表而非数组,这样无考虑扩容问题
// 构造方法,根据输入列表建堆
pub fn init(self: *Self, allocator: std.mem.Allocator, nums: []const T) !void {
@@ -82,7 +82,7 @@ pub fn MaxHeap(comptime T: type) type {
while (true) {
// 获取节点 i 的父节点
var p = parent(i);
// 当“越过根节点”或“节点无修复”时,结束堆化
// 当“越过根节点”或“节点无修复”时,结束堆化
if (p < 0 or self.max_heap.?.items[i] <= self.max_heap.?.items[p]) break;
// 交换两节点
try self.swap(i, p);
@@ -115,7 +115,7 @@ pub fn MaxHeap(comptime T: type) type {
var ma = i;
if (l < self.size() and self.max_heap.?.items[l] > self.max_heap.?.items[ma]) ma = l;
if (r < self.size() and self.max_heap.?.items[r] > self.max_heap.?.items[ma]) ma = r;
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
// 若节点 i 最大或索引 l, r 越界,则无继续堆化,跳出
if (ma == i) break;
// 交换两节点
try self.swap(i, ma);
+1 -1
View File
@@ -103,7 +103,7 @@ pub fn AVLTree(comptime T: type) type {
return self.leftRotate(node);
}
}
// 平衡树,无旋转,直接返回
// 平衡树,无旋转,直接返回
return node;
}