* Sync zh and zh-hant versions.

* Bug fixes.
This commit is contained in:
Yudong Jin
2025-06-12 20:45:41 +08:00
committed by GitHub
parent f42cec0f88
commit db64108e5d
9 changed files with 31 additions and 41 deletions

View File

@ -572,7 +572,7 @@
/* 递归的空间复杂度为 O(n) */
void recur(int n) {
if (n == 1) return;
return recur(n - 1);
recur(n - 1);
}
```
@ -592,7 +592,7 @@
/* 递归的空间复杂度为 O(n) */
void recur(int n) {
if (n == 1) return;
return recur(n - 1);
recur(n - 1);
}
```
@ -721,7 +721,7 @@
/* 递归的空间复杂度为 O(n) */
void recur(int n) {
if (n == 1) return;
return recur(n - 1);
recur(n - 1);
}
```
@ -763,7 +763,7 @@
/* 递归的空间复杂度为 O(n) */
void recur(int n) {
if (n == 1) return;
return recur(n - 1);
recur(n - 1);
}
```

View File

@ -511,7 +511,7 @@ Consider the following code, the term "worst-case" in worst-case space complexit
/* Recursion O(n) */
void recur(int n) {
if (n == 1) return;
return recur(n - 1);
recur(n - 1);
}
```
@ -531,7 +531,7 @@ Consider the following code, the term "worst-case" in worst-case space complexit
/* Recursion O(n) */
void recur(int n) {
if (n == 1) return;
return recur(n - 1);
recur(n - 1);
}
```
@ -660,7 +660,7 @@ Consider the following code, the term "worst-case" in worst-case space complexit
/* Recursion O(n) */
void recur(int n) {
if (n == 1) return;
return recur(n - 1);
recur(n - 1);
}
```
@ -702,7 +702,7 @@ Consider the following code, the term "worst-case" in worst-case space complexit
/* Recursion O(n) */
void recur(int n) {
if (n == 1) return;
return recur(n - 1);
recur(n - 1);
}
```

View File

@ -8,10 +8,7 @@
// 簡單實現,無法用於排序物件
function countingSortNaive(nums) {
// 1. 統計陣列最大元素 m
let m = 0;
for (const num of nums) {
m = Math.max(m, num);
}
let m = Math.max(...nums);
// 2. 統計各數字的出現次數
// counter[num] 代表 num 的出現次數
const counter = new Array(m + 1).fill(0);
@ -31,10 +28,7 @@ function countingSortNaive(nums) {
// 完整實現,可排序物件,並且是穩定排序
function countingSort(nums) {
// 1. 統計陣列最大元素 m
let m = 0;
for (const num of nums) {
m = Math.max(m, num);
}
let m = Math.max(...nums);
// 2. 統計各數字的出現次數
// counter[num] 代表 num 的出現次數
const counter = new Array(m + 1).fill(0);

View File

@ -41,12 +41,7 @@ function countingSortDigit(nums, exp) {
/* 基數排序 */
function radixSort(nums) {
// 獲取陣列的最大元素,用於判斷最大位數
let m = Number.MIN_VALUE;
for (const num of nums) {
if (num > m) {
m = num;
}
}
let m = Math.max(... nums);
// 按照從低位到高位的順序走訪
for (let exp = 1; exp <= m; exp *= 10) {
// 對陣列元素的第 k 位執行計數排序

View File

@ -8,10 +8,7 @@
// 簡單實現,無法用於排序物件
function countingSortNaive(nums: number[]): void {
// 1. 統計陣列最大元素 m
let m = 0;
for (const num of nums) {
m = Math.max(m, num);
}
let m: number = Math.max(...nums);
// 2. 統計各數字的出現次數
// counter[num] 代表 num 的出現次數
const counter: number[] = new Array<number>(m + 1).fill(0);
@ -31,10 +28,7 @@ function countingSortNaive(nums: number[]): void {
// 完整實現,可排序物件,並且是穩定排序
function countingSort(nums: number[]): void {
// 1. 統計陣列最大元素 m
let m = 0;
for (const num of nums) {
m = Math.max(m, num);
}
let m: number = Math.max(...nums);
// 2. 統計各數字的出現次數
// counter[num] 代表 num 的出現次數
const counter: number[] = new Array<number>(m + 1).fill(0);

View File

@ -41,12 +41,7 @@ function countingSortDigit(nums: number[], exp: number): void {
/* 基數排序 */
function radixSort(nums: number[]): void {
// 獲取陣列的最大元素,用於判斷最大位數
let m = Number.MIN_VALUE;
for (const num of nums) {
if (num > m) {
m = num;
}
}
let m: number = Math.max(... nums);
// 按照從低位到高位的順序走訪
for (let exp = 1; exp <= m; exp *= 10) {
// 對陣列元素的第 k 位執行計數排序

View File

@ -572,7 +572,7 @@
/* 遞迴的空間複雜度為 O(n) */
void recur(int n) {
if (n == 1) return;
return recur(n - 1);
recur(n - 1);
}
```
@ -592,7 +592,7 @@
/* 遞迴的空間複雜度為 O(n) */
void recur(int n) {
if (n == 1) return;
return recur(n - 1);
recur(n - 1);
}
```
@ -721,7 +721,7 @@
/* 遞迴的空間複雜度為 O(n) */
void recur(int n) {
if (n == 1) return;
return recur(n - 1);
recur(n - 1);
}
```
@ -763,7 +763,7 @@
/* 遞迴的空間複雜度為 O(n) */
void recur(int n) {
if (n == 1) return;
return recur(n - 1);
recur(n - 1);
}
```

View File

@ -173,7 +173,19 @@
=== "Zig"
```zig title=""
const hello = [5]u8{ 'h', 'e', 'l', 'l', 'o' };
// 以上程式碼展示了定義一個字面量陣列的方式,其中你可以選擇指明陣列的大小或者使用 _ 代替。使用 _ 時Zig 會嘗試自動計算陣列的長度
const matrix_4x4 = [4][4]f32{
[_]f32{ 1.0, 0.0, 0.0, 0.0 },
[_]f32{ 0.0, 1.0, 0.0, 1.0 },
[_]f32{ 0.0, 0.0, 1.0, 0.0 },
[_]f32{ 0.0, 0.0, 0.0, 1.0 },
};
// 多維陣列(矩陣)實際上就是巢狀陣列,我們很容易就可以建立一個多維陣列出來
const array = [_:0]u8{ 1, 2, 3, 4 };
// 定義一個哨兵終止陣列,本質上來說,這是為了相容 C 中的規定的字串結尾字元\0。我們使用語法 [N:x]T 來描述一個元素為型別 T長度為 N 的陣列,在它對應 N 的索引處的值應該是 x
```
??? pythontutor "視覺化執行"

View File

@ -80,4 +80,4 @@
- 二分搜尋僅適用於有序資料。若輸入資料無序,為了使用二分搜尋而專門進行排序,得不償失。因為排序演算法的時間複雜度通常為 $O(n \log n)$ ,比線性查詢和二分搜尋都更高。對於頻繁插入元素的場景,為保持陣列有序性,需要將元素插入到特定位置,時間複雜度為 $O(n)$ ,也是非常昂貴的。
- 二分搜尋僅適用於陣列。二分搜尋需要跳躍式(非連續地)訪問元素,而在鏈結串列中執行跳躍式訪問的效率較低,因此不適合應用在鏈結串列或基於鏈結串列實現的資料結構。
- 小資料量下,線性查詢效能更佳。線性查詢中,每輪只需 1 次判斷操作;而在二分搜尋中,需要 1 次加法、1 次除法、1 ~ 3 次判斷操作、1 次加法(減法),共 4 ~ 6 個單元操作;因此,當資料量 $n$ 較小時,線性查詢反而比二分搜尋更快。
- 小資料量下,線性查詢效能更佳。線性查詢中,每輪只需 1 次判斷操作;而在二分搜尋中,需要 1 次加法、1 次除法、1 ~ 3 次判斷操作、1 次加法(減法),共 4 ~ 6 個單元操作;因此,當資料量 $n$ 較小時,線性查詢反而比二分搜尋更快。