This commit is contained in:
krahets
2024-03-20 03:24:11 +08:00
parent 54c7448946
commit 35a07170c0
4 changed files with 172 additions and 112 deletions

View File

@ -1466,7 +1466,7 @@ Quadratic order means the number of operations grows quadratically with the inpu
def quadratic(n: int) -> int: def quadratic(n: int) -> int:
"""平方阶""" """平方阶"""
count = 0 count = 0
# 循环次数与数组长度成平方关系 # 循环次数与数据大小 n 成平方关系
for i in range(n): for i in range(n):
for j in range(n): for j in range(n):
count += 1 count += 1
@ -1479,7 +1479,7 @@ Quadratic order means the number of operations grows quadratically with the inpu
/* 平方阶 */ /* 平方阶 */
int quadratic(int n) { int quadratic(int n) {
int count = 0; int count = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
count++; count++;
@ -1495,7 +1495,7 @@ Quadratic order means the number of operations grows quadratically with the inpu
/* 平方阶 */ /* 平方阶 */
int quadratic(int n) { int quadratic(int n) {
int count = 0; int count = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
count++; count++;
@ -1511,7 +1511,7 @@ Quadratic order means the number of operations grows quadratically with the inpu
/* 平方阶 */ /* 平方阶 */
int Quadratic(int n) { int Quadratic(int n) {
int count = 0; int count = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
count++; count++;
@ -1527,7 +1527,7 @@ Quadratic order means the number of operations grows quadratically with the inpu
/* 平方阶 */ /* 平方阶 */
func quadratic(n int) int { func quadratic(n int) int {
count := 0 count := 0
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
for j := 0; j < n; j++ { for j := 0; j < n; j++ {
count++ count++
@ -1543,7 +1543,7 @@ Quadratic order means the number of operations grows quadratically with the inpu
/* 平方阶 */ /* 平方阶 */
func quadratic(n: Int) -> Int { func quadratic(n: Int) -> Int {
var count = 0 var count = 0
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for _ in 0 ..< n { for _ in 0 ..< n {
for _ in 0 ..< n { for _ in 0 ..< n {
count += 1 count += 1
@ -1559,7 +1559,7 @@ Quadratic order means the number of operations grows quadratically with the inpu
/* 平方阶 */ /* 平方阶 */
function quadratic(n) { function quadratic(n) {
let count = 0; let count = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) { for (let j = 0; j < n; j++) {
count++; count++;
@ -1575,7 +1575,7 @@ Quadratic order means the number of operations grows quadratically with the inpu
/* 平方阶 */ /* 平方阶 */
function quadratic(n: number): number { function quadratic(n: number): number {
let count = 0; let count = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) { for (let j = 0; j < n; j++) {
count++; count++;
@ -1591,7 +1591,7 @@ Quadratic order means the number of operations grows quadratically with the inpu
/* 平方阶 */ /* 平方阶 */
int quadratic(int n) { int quadratic(int n) {
int count = 0; int count = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
count++; count++;
@ -1607,7 +1607,7 @@ Quadratic order means the number of operations grows quadratically with the inpu
/* 平方阶 */ /* 平方阶 */
fn quadratic(n: i32) -> i32 { fn quadratic(n: i32) -> i32 {
let mut count = 0; let mut count = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for _ in 0..n { for _ in 0..n {
for _ in 0..n { for _ in 0..n {
count += 1; count += 1;
@ -1623,7 +1623,7 @@ Quadratic order means the number of operations grows quadratically with the inpu
/* 平方阶 */ /* 平方阶 */
int quadratic(int n) { int quadratic(int n) {
int count = 0; int count = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
count++; count++;
@ -1640,7 +1640,7 @@ Quadratic order means the number of operations grows quadratically with the inpu
fn quadratic(n: i32) i32 { fn quadratic(n: i32) i32 {
var count: i32 = 0; var count: i32 = 0;
var i: i32 = 0; var i: i32 = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
while (i < n) : (i += 1) { while (i < n) : (i += 1) {
var j: i32 = 0; var j: i32 = 0;
while (j < n) : (j += 1) { while (j < n) : (j += 1) {
@ -1653,8 +1653,8 @@ Quadratic order means the number of operations grows quadratically with the inpu
??? pythontutor "Code Visualization" ??? pythontutor "Code Visualization"
<div style="height: 477px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20quadratic%28n%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%B9%B3%E6%96%B9%E9%98%B6%22%22%22%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E6%AC%A1%E6%95%B0%E4%B8%8E%E6%95%B0%E7%BB%84%E9%95%BF%E5%BA%A6%E6%88%90%E5%B9%B3%E6%96%B9%E5%85%B3%E7%B3%BB%0A%20%20%20%20for%20i%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20return%20count%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20quadratic%28n%29%0A%20%20%20%20print%28%22%E5%B9%B3%E6%96%B9%E9%98%B6%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div> <div style="height: 477px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20quadratic%28n%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%B9%B3%E6%96%B9%E9%98%B6%22%22%22%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E6%AC%A1%E6%95%B0%E4%B8%8E%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%E6%88%90%E5%B9%B3%E6%96%B9%E5%85%B3%E7%B3%BB%0A%20%20%20%20for%20i%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20return%20count%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20quadratic%28n%29%0A%20%20%20%20print%28%22%E5%B9%B3%E6%96%B9%E9%98%B6%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20quadratic%28n%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%B9%B3%E6%96%B9%E9%98%B6%22%22%22%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E6%AC%A1%E6%95%B0%E4%B8%8E%E6%95%B0%E7%BB%84%E9%95%BF%E5%BA%A6%E6%88%90%E5%B9%B3%E6%96%B9%E5%85%B3%E7%B3%BB%0A%20%20%20%20for%20i%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20return%20count%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20quadratic%28n%29%0A%20%20%20%20print%28%22%E5%B9%B3%E6%96%B9%E9%98%B6%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">Full Screen ></a></div> <div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20quadratic%28n%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%B9%B3%E6%96%B9%E9%98%B6%22%22%22%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E6%AC%A1%E6%95%B0%E4%B8%8E%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%E6%88%90%E5%B9%B3%E6%96%B9%E5%85%B3%E7%B3%BB%0A%20%20%20%20for%20i%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20return%20count%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20quadratic%28n%29%0A%20%20%20%20print%28%22%E5%B9%B3%E6%96%B9%E9%98%B6%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">Full Screen ></a></div>
The following image compares constant order, linear order, and quadratic order time complexities. The following image compares constant order, linear order, and quadratic order time complexities.

File diff suppressed because one or more lines are too long

View File

@ -4,74 +4,134 @@ comments: true
# 16.3 &nbsp; 术语表 # 16.3 &nbsp; 术语表
表 16-1 列出了书中出现的重要术语。建议读者同时记住它们的中英文叫法,以便阅读英文文献 表 16-1 列出了书中出现的重要术语,值得注意以下几点
- 部分名词在简体中文和繁体中文下的叫法不同。
- 建议记住名词的英文叫法,以便阅读英文文献。
<p align="center"> 表 16-1 &nbsp; 数据结构与算法的重要名词 </p> <p align="center"> 表 16-1 &nbsp; 数据结构与算法的重要名词 </p>
<div class="center-table" markdown> <div class="center-table" markdown>
| 中文 | English | 中文 | English | | English | 简体中文 | 繁体中文 |
| -------------- | ------------------------------ | -------------- | --------------------------- | | ------------------------------ | -------------- | -------------- |
| 算法 | algorithm | AVL 树 | AVL tree | | algorithm | 算法 | 算法 |
| 数据结构 | data structure | 红黑树 | red-black tree | | data structure | 数据结构 | 資料結構 |
| 渐近复杂度分析 | asymptotic complexity analysis | 层序遍历 | level-order traversal | | asymptotic complexity analysis | 渐近复杂度分析 | 漸近複雜度分析 |
| 时间复杂度 | time complexity | 广度优先遍历 | breadth-first traversal | | time complexity | 时间复杂度 | 時間複雜度 |
| 空间复杂度 | space complexity | 深度优先遍历 | depth-first traversal | | space complexity | 空间复杂度 | 空間複雜度 |
| 迭代 | iteration | 二叉搜索树 | binary search tree | | iteration | 迭代 | 迭代 |
| 递归 | recursion | 平衡二叉搜索树 | balanced binary search tree | | recursion | 递归 | 遞迴 |
| 尾递归 | tail recursion | 平衡因子 | balance factor | | tail recursion | 尾递归 | 尾遞迴 |
| 递归树 | recursion tree | | heap | | recursion tree | 递归树 | 遞迴樹 |
| 大 $O$ 记号 | big-$O$ notation | 大顶堆 | max heap | | big-$O$ notation | 大 $O$ 记号 | 大 $O$ 記號 |
| 渐近上界 | asymptotic upper bound | 小顶堆 | min heap | | asymptotic upper bound | 渐近上界 | 漸近上界 |
| 原码 | sign-magnitude | 优先队列 | priority queue | | sign-magnitude | 原码 | 原碼 |
| 反码 | 1s complement | 堆化 | heapify | | 1s complement | 反码 | 反碼 |
| 补码 | 2s complement | Top-$k$ 问题 | Top-$k$ problem | | 2s complement | 补码 | 補碼 |
| 数组 | array | | graph | | array | 数组 | 陣列 |
| 索引 | index | 顶点 | vertex | | index | 索引 | 索引 |
| 链表 | linked list | 无向图 | undirected graph | | linked list | 链表 | 連結串列 |
| 链表节点 | linked list node, list node | 有向图 | directed graph | | linked list node, list node | 链表节点 | 連結串列節點 |
| 头节点 | head node | 连通图 | connected graph | | head node | 头节点 | 頭節點 |
| 尾节点 | tail node | 非连通图 | disconnected graph | | tail node | 尾节点 | 尾節點 |
| 列表 | list | 有权图 | weighted graph | | list | 列表 | 列表 |
| 动态数组 | dynamic array | 邻接 | adjacency | | dynamic array | 动态数组 | 動態陣列 |
| 硬盘 | hard disk | 路径 | path | | hard disk | 硬盘 | 硬碟 |
| 内存 | random-access memory (RAM) | 入度 | in-degree | | random-access memory (RAM) | 内存 | 內存 |
| 缓存 | cache memory | 出度 | out-degree | | cache memory | 缓存 | 快取 |
| 缓存未命中 | cache miss | 邻接矩阵 | adjacency matrix | | cache miss | 缓存未命中 | 快取未命中 |
| 缓存命中率 | cache hit rate | 邻接表 | adjacency list | | cache hit rate | 缓存命中率 | 快取命中率 |
| 栈 | stack | 广度优先搜索 | breadth-first search | | stack | 栈 | 堆疊 |
| 栈顶 | top of the stack | 深度优先搜索 | depth-first search | | top of the stack | 栈顶 | 堆疊頂 |
| 栈底 | bottom of the stack | 二分查找 | binary search | | bottom of the stack | 栈底 | 堆疊底 |
| 队列 | queue | 搜索算法 | searching algorithm | | queue | 队列 | 佇列 |
| 双向队列 | double-ended queue | 排序算法 | sorting algorithm | | double-ended queue | 双向队列 | 雙向佇列 |
| 队首 | front of the queue | 选择排序 | selection sort | | front of the queue | 队首 | 佇列首 |
| 队尾 | rear of the queue | 冒泡排序 | bubble sort | | rear of the queue | 队尾 | 佇列尾 |
| 哈希表 | hash table | 插入排序 | insertion sort | | hash table | 哈希表 | 雜湊表 |
| 桶 | bucket | 快速排序 | quick sort | | bucket | | 桶 |
| 哈希函数 | hash function | 归并排序 | merge sort | | hash function | 哈希函数 | 雜湊函式 |
| 哈希冲突 | hash collision | 堆排序 | heap sort | | hash collision | 哈希冲突 | 雜湊衝突 |
| 负载因子 | load factor | 桶排序 | bucket sort | | load factor | 负载因子 | 負載因子 |
| 链式地址 | separate chaining | 计数排序 | counting sort | | separate chaining | 链式地址 | 鏈結位址 |
| 开放寻址 | open addressing | 基数排序 | radix sort | | open addressing | 开放寻址 | 開放定址 |
| 线性探测 | linear probing | 分治 | divide and conquer | | linear probing | 线性探测 | 線性探查 |
| 懒删除 | lazy deletion | 汉诺塔问题 | hanota problem | | lazy deletion | 懒删除 | 懶刪除 |
| 二叉树 | binary tree | 回溯算法 | backtracking algorithm | | binary tree | 二叉树 | 二元樹 |
| 树节点 | tree node | 约束 | constraint | | tree node | 树节点 | 樹節點 |
| 左子节点 | left-child node | | solution | | left-child node | 左子节点 | 左子節點 |
| 右子节点 | right-child node | 状态 | state | | right-child node | 右子节点 | 右子節點 |
| 父节点 | parent node | 剪枝 | pruning | | parent node | 父节点 | 父節點 |
| 左子树 | left subtree | 全排列问题 | permutations problem | | left subtree | 左子树 | 左子樹 |
| 右子树 | right subtree | 子集和问题 | subset-sum problem | | right subtree | 右子树 | 右子樹 |
| 根节点 | root node | n 皇后问题 | n-queens problem | | root node | 根节点 | 根節點 |
| 叶节点 | leaf node | 动态规划 | dynamic programming | | leaf node | 叶节点 | 葉節點 |
| 边 | edge | 初始状态 | initial state | | edge | | 邊 |
| 层 | level | 状态转移方程 | state-trasition equation | | level | 层 | 層 |
| 度 | degree | 背包问题 | knapsack problem | | degree | | |
| 高度 | height | 编辑距离问题 | edit distance problem | | height | 高度 | 高度 |
| 深度 | depth | 贪心算法 | greedy algorithm | | depth | 深度 | 深度 |
| 完美二叉树 | perfect binary tree | | | | perfect binary tree | 完美二叉树 | 完美二元樹 |
| 完全二叉树 | complete binary tree | | | | complete binary tree | 完全二叉树 | 完全二元樹 |
| 完满二叉树 | full binary tree | | | | full binary tree | 完满二叉树 | 完滿二元樹 |
| 平衡二叉树 | balanced binary tree | | | | balanced binary tree | 平衡二叉树 | 平衡二元樹 |
| AVL tree | AVL 树 | AVL 樹 |
| red-black tree | 红黑树 | 紅黑樹 |
| level-order traversal | 层序遍历 | 層序走訪 |
| breadth-first traversal | 广度优先遍历 | 廣度優先走訪 |
| depth-first traversal | 深度优先遍历 | 深度優先走訪 |
| binary search tree | 二叉搜索树 | 二元搜尋樹 |
| balanced binary search tree | 平衡二叉搜索树 | 平衡二元搜尋樹 |
| balance factor | 平衡因子 | 平衡因子 |
| heap | 堆 | 堆 |
| max heap | 大顶堆 | 大頂堆 |
| min heap | 小顶堆 | 小頂堆 |
| priority queue | 优先队列 | 優先佇列 |
| heapify | 堆化 | 堆化 |
| top-$k$ problem | Top-$k$ 问题 | Top-$k$ 問題 |
| graph | 图 | 圖 |
| vertex | 顶点 | 頂點 |
| undirected graph | 无向图 | 無向圖 |
| directed graph | 有向图 | 有向圖 |
| connected graph | 连通图 | 連通圖 |
| disconnected graph | 非连通图 | 非連通圖 |
| weighted graph | 有权图 | 有權圖 |
| adjacency | 邻接 | 鄰接 |
| path | 路径 | 路徑 |
| in-degree | 入度 | 入度 |
| out-degree | 出度 | 出度 |
| adjacency matrix | 邻接矩阵 | 鄰接矩陣 |
| adjacency list | 邻接表 | 鄰接表 |
| breadth-first search | 广度优先搜索 | 廣度優先搜尋 |
| depth-first search | 深度优先搜索 | 深度優先搜尋 |
| binary search | 二分查找 | 二分查找 |
| searching algorithm | 搜索算法 | 搜尋演算法 |
| sorting algorithm | 排序算法 | 排序演算法 |
| selection sort | 选择排序 | 選擇排序 |
| bubble sort | 冒泡排序 | 泡沫排序 |
| insertion sort | 插入排序 | 插入排序 |
| quick sort | 快速排序 | 快速排序 |
| merge sort | 归并排序 | 合併排序 |
| heap sort | 堆排序 | 堆排序 |
| bucket sort | 桶排序 | 桶排序 |
| counting sort | 计数排序 | 計數排序 |
| radix sort | 基数排序 | 基數排序 |
| divide and conquer | 分治 | 分治 |
| hanota problem | 汉诺塔问题 | 漢諾塔問題 |
| backtracking algorithm | 回溯算法 | 回溯演算法 |
| constraint | 约束 | 約束 |
| solution | 解 | 解 |
| state | 状态 | 狀態 |
| pruning | 剪枝 | 剪枝 |
| permutations problem | 全排列问题 | 全排列問題 |
| subset-sum problem | 子集和问题 | 子集合問題 |
| $n$-queens problem | $n$ 皇后问题 | $n$ 皇后問題 |
| dynamic programming | 动态规划 | 動態規劃 |
| initial state | 初始状态 | 初始狀態 |
| state-trasition equation | 状态转移方程 | 狀態轉移方程 |
| knapsack problem | 背包问题 | 背包問題 |
| edit distance problem | 编辑距离问题 | 編輯距離問題 |
| greedy algorithm | 贪心算法 | 貪心演算法 |
</div> </div>

View File

@ -1470,7 +1470,7 @@ $$
def quadratic(n: int) -> int: def quadratic(n: int) -> int:
"""平方阶""" """平方阶"""
count = 0 count = 0
# 循环次数与数组长度成平方关系 # 循环次数与数据大小 n 成平方关系
for i in range(n): for i in range(n):
for j in range(n): for j in range(n):
count += 1 count += 1
@ -1483,7 +1483,7 @@ $$
/* 平方阶 */ /* 平方阶 */
int quadratic(int n) { int quadratic(int n) {
int count = 0; int count = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
count++; count++;
@ -1499,7 +1499,7 @@ $$
/* 平方阶 */ /* 平方阶 */
int quadratic(int n) { int quadratic(int n) {
int count = 0; int count = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
count++; count++;
@ -1515,7 +1515,7 @@ $$
/* 平方阶 */ /* 平方阶 */
int Quadratic(int n) { int Quadratic(int n) {
int count = 0; int count = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
count++; count++;
@ -1531,7 +1531,7 @@ $$
/* 平方阶 */ /* 平方阶 */
func quadratic(n int) int { func quadratic(n int) int {
count := 0 count := 0
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
for j := 0; j < n; j++ { for j := 0; j < n; j++ {
count++ count++
@ -1547,7 +1547,7 @@ $$
/* 平方阶 */ /* 平方阶 */
func quadratic(n: Int) -> Int { func quadratic(n: Int) -> Int {
var count = 0 var count = 0
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for _ in 0 ..< n { for _ in 0 ..< n {
for _ in 0 ..< n { for _ in 0 ..< n {
count += 1 count += 1
@ -1563,7 +1563,7 @@ $$
/* 平方阶 */ /* 平方阶 */
function quadratic(n) { function quadratic(n) {
let count = 0; let count = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) { for (let j = 0; j < n; j++) {
count++; count++;
@ -1579,7 +1579,7 @@ $$
/* 平方阶 */ /* 平方阶 */
function quadratic(n: number): number { function quadratic(n: number): number {
let count = 0; let count = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) { for (let j = 0; j < n; j++) {
count++; count++;
@ -1595,7 +1595,7 @@ $$
/* 平方阶 */ /* 平方阶 */
int quadratic(int n) { int quadratic(int n) {
int count = 0; int count = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
count++; count++;
@ -1611,7 +1611,7 @@ $$
/* 平方阶 */ /* 平方阶 */
fn quadratic(n: i32) -> i32 { fn quadratic(n: i32) -> i32 {
let mut count = 0; let mut count = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for _ in 0..n { for _ in 0..n {
for _ in 0..n { for _ in 0..n {
count += 1; count += 1;
@ -1627,7 +1627,7 @@ $$
/* 平方阶 */ /* 平方阶 */
int quadratic(int n) { int quadratic(int n) {
int count = 0; int count = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
count++; count++;
@ -1644,7 +1644,7 @@ $$
fn quadratic(n: i32) i32 { fn quadratic(n: i32) i32 {
var count: i32 = 0; var count: i32 = 0;
var i: i32 = 0; var i: i32 = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数据大小 n 成平方关系
while (i < n) : (i += 1) { while (i < n) : (i += 1) {
var j: i32 = 0; var j: i32 = 0;
while (j < n) : (j += 1) { while (j < n) : (j += 1) {
@ -1657,8 +1657,8 @@ $$
??? pythontutor "可视化运行" ??? pythontutor "可视化运行"
<div style="height: 477px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20quadratic%28n%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%B9%B3%E6%96%B9%E9%98%B6%22%22%22%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E6%AC%A1%E6%95%B0%E4%B8%8E%E6%95%B0%E7%BB%84%E9%95%BF%E5%BA%A6%E6%88%90%E5%B9%B3%E6%96%B9%E5%85%B3%E7%B3%BB%0A%20%20%20%20for%20i%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20return%20count%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20quadratic%28n%29%0A%20%20%20%20print%28%22%E5%B9%B3%E6%96%B9%E9%98%B6%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div> <div style="height: 477px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20quadratic%28n%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%B9%B3%E6%96%B9%E9%98%B6%22%22%22%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E6%AC%A1%E6%95%B0%E4%B8%8E%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%E6%88%90%E5%B9%B3%E6%96%B9%E5%85%B3%E7%B3%BB%0A%20%20%20%20for%20i%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20return%20count%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20quadratic%28n%29%0A%20%20%20%20print%28%22%E5%B9%B3%E6%96%B9%E9%98%B6%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20quadratic%28n%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%B9%B3%E6%96%B9%E9%98%B6%22%22%22%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E6%AC%A1%E6%95%B0%E4%B8%8E%E6%95%B0%E7%BB%84%E9%95%BF%E5%BA%A6%E6%88%90%E5%B9%B3%E6%96%B9%E5%85%B3%E7%B3%BB%0A%20%20%20%20for%20i%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20return%20count%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20quadratic%28n%29%0A%20%20%20%20print%28%22%E5%B9%B3%E6%96%B9%E9%98%B6%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div> <div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20quadratic%28n%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%B9%B3%E6%96%B9%E9%98%B6%22%22%22%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E6%AC%A1%E6%95%B0%E4%B8%8E%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%E6%88%90%E5%B9%B3%E6%96%B9%E5%85%B3%E7%B3%BB%0A%20%20%20%20for%20i%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20return%20count%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20quadratic%28n%29%0A%20%20%20%20print%28%22%E5%B9%B3%E6%96%B9%E9%98%B6%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div>
图 2-10 对比了常数阶、线性阶和平方阶三种时间复杂度。 图 2-10 对比了常数阶、线性阶和平方阶三种时间复杂度。