mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-03 05:27:55 +08:00
Polish the chapter
introduction, computational complexity.
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
// 交换两节点
|
||||
|
||||
@ -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;
|
||||
// 交换两节点
|
||||
|
||||
@ -87,7 +87,7 @@ class AVLTree {
|
||||
return leftRotate(node);
|
||||
}
|
||||
}
|
||||
// 平衡树,无需旋转,直接返回
|
||||
// 平衡树,无须旋转,直接返回
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user