Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare 1.1.0 release #1274

Merged
merged 12 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions codes/c/chapter_sorting/bucket_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,20 @@ void bucketSort(float nums[], int n) {
int k = n / 2; // 初始化 k = n/2 个桶
int *sizes = malloc(k * sizeof(int)); // 记录每个桶的大小
float **buckets = malloc(k * sizeof(float *)); // 动态数组的数组(桶)

// 为每个桶预分配足够的空间
for (int i = 0; i < k; ++i) {
// 为每个桶预分配足够的空间
buckets[i] = (float *)malloc(n * sizeof(float));
sizes[i] = 0;
}

// 1. 将数组元素分配到各个桶中
for (int i = 0; i < n; ++i) {
int idx = (int)(nums[i] * k);
buckets[idx][sizes[idx]++] = nums[i];
}

// 2. 对各个桶执行排序
for (int i = 0; i < k; ++i) {
qsort(buckets[i], sizes[i], sizeof(float), compare);
}

// 3. 合并排序后的桶
int idx = 0;
for (int i = 0; i < k; ++i) {
Expand Down
37 changes: 20 additions & 17 deletions codes/c/chapter_sorting/quick_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@ void swap(int nums[], int i, int j) {
nums[j] = tmp;
}

/* 快速排序类 */
// 快速排序类-哨兵划分
/* 哨兵划分 */
int partition(int nums[], int left, int right) {
// 以 nums[left] 为基准数
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left]) {
// 从右向左找首个小于基准数的元素
j--;
j--; // 从右向左找首个小于基准数的元素
}
while (i < j && nums[i] <= nums[left]) {
// 从左向右找首个大于基准数的元素
i++;
i++; // 从左向右找首个大于基准数的元素
}
// 交换这两个元素
swap(nums, i, j);
Expand All @@ -36,7 +33,7 @@ int partition(int nums[], int left, int right) {
return i;
}

// 快速排序类-快速排序
/* 快速排序 */
void quickSort(int nums[], int left, int right) {
// 子数组长度为 1 时终止递归
if (left >= right) {
Expand All @@ -49,8 +46,9 @@ void quickSort(int nums[], int left, int right) {
quickSort(nums, pivot + 1, right);
}

/* 快速排序类(中位基准数优化) */
// 选取三个候选元素的中位数
// 以下为中位数优化的快速排序

/* 选取三个候选元素的中位数 */
int medianThree(int nums[], int left, int mid, int right) {
int l = nums[left], m = nums[mid], r = nums[right];
if ((l <= m && m <= r) || (r <= m && m <= l))
Expand All @@ -60,7 +58,7 @@ int medianThree(int nums[], int left, int mid, int right) {
return right;
}

/* 哨兵划分(三数取中值) */
/* 哨兵划分(三数取中值) */
int partitionMedian(int nums[], int left, int right) {
// 选取三个候选元素的中位数
int med = medianThree(nums, left, (left + right) / 2, right);
Expand All @@ -79,7 +77,7 @@ int partitionMedian(int nums[], int left, int right) {
return i; // 返回基准数的索引
}

// 中位基准数优化-快速排序
/* 快速排序(三数取中值) */
void quickSortMedian(int nums[], int left, int right) {
// 子数组长度为 1 时终止递归
if (left >= right)
Expand All @@ -91,20 +89,25 @@ void quickSortMedian(int nums[], int left, int right) {
quickSortMedian(nums, pivot + 1, right);
}

/* 快速排序类(尾递归优化) */
// 快速排序(尾递归优化)
// 以下为尾递归优化的快速排序

/* 快速排序(尾递归优化) */
void quickSortTailCall(int nums[], int left, int right) {
// 子数组长度为 1 时终止
while (left < right) {
// 哨兵划分操作
int pivot = partition(nums, left, right);
// 对两个子数组中较短的那个执行快速排序
if (pivot - left < right - pivot) {
quickSortTailCall(nums, left, pivot - 1); // 递归排序左子数组
left = pivot + 1; // 剩余未排序区间为 [pivot + 1, right]
// 递归排序左子数组
quickSortTailCall(nums, left, pivot - 1);
// 剩余未排序区间为 [pivot + 1, right]
left = pivot + 1;
} else {
quickSortTailCall(nums, pivot + 1, right); // 递归排序右子数组
right = pivot - 1; // 剩余未排序区间为 [left, pivot - 1]
// 递归排序右子数组
quickSortTailCall(nums, pivot + 1, right);
// 剩余未排序区间为 [left, pivot - 1]
right = pivot - 1;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions docs/chapter_preface/about_the_book.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@

## 致谢

本书在开源社区众多贡献者的共同努力下不断完善。感谢每一位投入时间与精力的撰稿人,他们是(按照 GitHub 自动生成的顺序):krahets、codingonion、nuomi1、Gonglja、Reanon、justin-tse、danielsss、hpstory、S-N-O-R-L-A-X、night-cruise、msk397、gvenusleo、RiverTwilight、gyt95、zhuoqinyue、Zuoxun、Xia-Sang、mingXta、FangYuan33、GN-Yu、IsChristina、xBLACKICEx、guowei-gong、Cathay-Chen、mgisr、JoseHung、qualifier1024、pengchzn、Guanngxu、longsizhuo、L-Super、what-is-me、yuan0221、lhxsm、Slone123c、WSL0809、longranger2、theNefelibatas、xiongsp、JeffersonHuang、hongyun-robot、K3v123、yuelinxin、a16su、gaofer、malone6、Wonderdch、xjr7670、DullSword、Horbin-Magician、NI-SW、reeswell、XC-Zero、XiaChuerwu、yd-j、iron-irax、huawuque404、MolDuM、Nigh、KorsChen、foursevenlove、52coder、bubble9um、youshaoXG、curly210102、gltianwen、fanchenggang、Transmigration-zhou、FloranceYeh、FreddieLi、ShiMaRing、lipusheng、Javesun99、JackYang-hellobobo、shanghai-Jerry、0130w、Keynman、psychelzh、logan-qiu、ZnYang2018、MwumLi、1ch0、Phoenix0415、qingpeng9802、Richard-Zhang1019、QiLOL、Suremotoo、Turing-1024-Lee、Evilrabbit520、GaochaoZhu、ZJKung、linzeyan、hezhizhen、ZongYangL、beintentional、czruby、coderlef、dshlstarr、szu17dmy、fbigm、gledfish、hts0000、boloboloda、iStig、jiaxianhua、wenjianmin、keshida、kilikilikid、lclc6、lwbaptx、liuxjerry、lucaswangdev、lyl625760、chadyi、noobcodemaker、selear、siqyka、syd168、4yDX3906、tao363、wangwang105、weibk、yabo083、yi427、yishangzhang、zhouLion、baagod、ElaBosak233、xb534、luluxia、yanedie、thomasq0、YangXuanyi 和 th1nk3r-ing 。
本书在开源社区众多贡献者的共同努力下不断完善。感谢每一位投入时间与精力的撰稿人,他们是(按照 GitHub 自动生成的顺序):krahets、Gonglja、nuomi1、codingonion、Reanon、justin-tse、hpstory、danielsss、curtishd、night-cruise、S-N-O-R-L-A-X、msk397、gvenusleo、RiverTwilight、gyt95、zhuoqinyue、Zuoxun、mingXta、hello-ikun、khoaxuantu、FangYuan33、GN-Yu、longsizhuo、mgisr、Cathay-Chen、guowei-gong、xBLACKICEx、K3v123、IsChristina、JoseHung、qualifier1024、pengchzn、Guanngxu、QiLOL、L-Super、WSL0809、Slone123c、lhxsm、yuan0221、what-is-me、rongyi、JeffersonHuang、longranger2、theNefelibatas、yuelinxin、xiongsp、nanlei、a16su、cy-by-side、gaofer、malone6、Wonderdch、hongyun-robot、XiaChuerwu、yd-j、bluebean-cloud、iron-irax、he-weilai、Nigh、MolDuM、Phoenix0415、XC-Zero、SamJin98、reeswell、NI-SW、Horbin-Magician、xjr7670、YangXuanyi、DullSword、iStig、qq909244296、jiaxianhua、wenjianmin、keshida、kilikilikid、lclc6、lwbaptx、luluxia、boloboloda、hts0000、gledfish、fbigm、echo1937、szu17dmy、dshlstarr、coderlef、czruby、beintentional、KeiichiKasai、xb534、ElaBosak233、baagod、zhouLion、yishangzhang、yi427、yabo083、weibk、wangwang105、th1nk3r-ing、tao363、4yDX3906、syd168、siqyka、selear、sdshaoda、noobcodemaker、chadyi、lyl625760、lucaswangdev、liuxjerry、0130w、shanghai-Jerry、JackYang-hellobobo、Javesun99、lipusheng、ShiMaRing、FreddieLi、FloranceYeh、Transmigration-zhou、fanchenggang、gltianwen、Dr-XYZ、curly210102、CuB3y0nd、youshaoXG、bubble9um、fanenr、52coder、foursevenlove、KorsChen、ZongYangL、hezhizhen、linzeyan、ZJKung、GaochaoZhu、yang-le、Evilrabbit520、Turing-1024-Lee、Suremotoo、Allen-Scai、Richard-Zhang1019、qingpeng9802、primexiao、nidhoggfgg、1ch0、MwumLi、ZnYang2018、hugtyftg、logan-qiu、psychelzh 和 Keynman 。

本书的代码审阅工作由 codingonion、Gonglja、gvenusleo、hpstory、justin-tse、krahets、night-cruise、nuomi1 和 Reanon 完成(按照首字母顺序排列)。感谢他们付出的时间与精力,正是他们确保了各语言代码的规范与统一。
本书的代码审阅工作由 codingonion、curtishd、Gonglja、gvenusleo、hpstory、justin-tse、krahets、night-cruise、nuomi1 和 Reanon 完成(按照首字母顺序排列)。感谢他们付出的时间与精力,正是他们确保了各语言代码的规范与统一。

在本书的创作过程中,我得到了许多人的帮助。

Expand Down
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ site_dir: site
repo_name: krahets/hello-algo
repo_url: https://github.com/krahets/hello-algo
edit_uri: tree/main/docs
version: 1.0.0
version: 1.1.0

# Copyright
copyright: Copyright &copy; 2022-2024 krahets<br>The website content is licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>
Expand Down
2 changes: 1 addition & 1 deletion overrides/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{% elif config.theme.language == 'zh-Hant' %}
{% set announcements = '紙質書(簡體中文版)已發行,詳情請見<a href="/chapter_paperbook/">這裡</a>' %}
{% elif config.theme.language == 'en' %}
{% set announcements = 'Feel free to engage in Chinese-to-English translation and pull request review! Please visit <a href="https://github.com/krahets/hello-algo/issues/914">#914</a> for details.' %}
{% set announcements = 'Welcome to contribute to Chinese-to-English translation! Please visit <a href="https://github.com/krahets/hello-algo/issues/914">#914</a> for more details.' %}
{% endif %}
<div class="banner-svg">
<svg xmlns="http://www.w3.org/2000/svg"
Expand Down
6 changes: 5 additions & 1 deletion zh-hant/codes/c/chapter_dynamic_programming/coin_change.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ int coinChangeDPComp(int coins[], int amt, int coinsSize) {
int n = coinsSize;
int MAX = amt + 1;
// 初始化 dp 表
int *dp = calloc(amt + 1, sizeof(int));
int *dp = malloc((amt + 1) * sizeof(int));
for (int j = 1; j <= amt; j++) {
dp[j] = MAX;
}
dp[0] = 0;

// 狀態轉移
for (int i = 1; i <= n; i++) {
for (int a = 1; a <= amt; a++) {
Expand Down
4 changes: 2 additions & 2 deletions zh-hant/codes/c/chapter_hashing/array_hash_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "../utils/common.h"

/* 雜湊表預設大小 */
#define 100
#define MAX_SIZE 100

/* 鍵值對 int->string */
typedef struct {
Expand All @@ -29,7 +29,7 @@ typedef struct {
/* 建構子 */
ArrayHashMap *newArrayHashMap() {
ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));
for (int i = 0; i < MAX_SIZE; i++) {
for (int i=0; i < MAX_SIZE; i++) {
hmap->buckets[i] = NULL;
}
return hmap;
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/c/chapter_heap/my_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int right(MaxHeap *maxHeap, int i) {

/* 獲取父節點的索引 */
int parent(MaxHeap *maxHeap, int i) {
return (i - 1) / 2;
return (i - 1) / 2; // 向下取整
}

/* 交換元素 */
Expand Down
6 changes: 1 addition & 5 deletions zh-hant/codes/c/chapter_sorting/bucket_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,20 @@ void bucketSort(float nums[], int n) {
int k = n / 2; // 初始化 k = n/2 個桶
int *sizes = malloc(k * sizeof(int)); // 記錄每個桶的大小
float **buckets = malloc(k * sizeof(float *)); // 動態陣列的陣列(桶)

// 為每個桶預分配足夠的空間
for (int i = 0; i < k; ++i) {
// 為每個桶預分配足夠的空間
buckets[i] = (float *)malloc(n * sizeof(float));
sizes[i] = 0;
}

// 1. 將陣列元素分配到各個桶中
for (int i = 0; i < n; ++i) {
int idx = (int)(nums[i] * k);
buckets[idx][sizes[idx]++] = nums[i];
}

// 2. 對各個桶執行排序
for (int i = 0; i < k; ++i) {
qsort(buckets[i], sizes[i], sizeof(float), compare);
}

// 3. 合併排序後的桶
int idx = 0;
for (int i = 0; i < k; ++i) {
Expand Down
37 changes: 20 additions & 17 deletions zh-hant/codes/c/chapter_sorting/quick_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@ void swap(int nums[], int i, int j) {
nums[j] = tmp;
}

/* 快速排序類別 */
// 快速排序類別-哨兵劃分
/* 哨兵劃分 */
int partition(int nums[], int left, int right) {
// 以 nums[left] 為基準數
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left]) {
// 從右向左找首個小於基準數的元素
j--;
j--; // 從右向左找首個小於基準數的元素
}
while (i < j && nums[i] <= nums[left]) {
// 從左向右找首個大於基準數的元素
i++;
i++; // 從左向右找首個大於基準數的元素
}
// 交換這兩個元素
swap(nums, i, j);
Expand All @@ -36,7 +33,7 @@ int partition(int nums[], int left, int right) {
return i;
}

// 快速排序類別-快速排序
/* 快速排序 */
void quickSort(int nums[], int left, int right) {
// 子陣列長度為 1 時終止遞迴
if (left >= right) {
Expand All @@ -49,8 +46,9 @@ void quickSort(int nums[], int left, int right) {
quickSort(nums, pivot + 1, right);
}

/* 快速排序類別(中位基準數最佳化) */
// 選取三個候選元素的中位數
// 以下為中位數最佳化的快速排序

/* 選取三個候選元素的中位數 */
int medianThree(int nums[], int left, int mid, int right) {
int l = nums[left], m = nums[mid], r = nums[right];
if ((l <= m && m <= r) || (r <= m && m <= l))
Expand All @@ -60,7 +58,7 @@ int medianThree(int nums[], int left, int mid, int right) {
return right;
}

/* 哨兵劃分(三數取中值) */
/* 哨兵劃分(三數取中值) */
int partitionMedian(int nums[], int left, int right) {
// 選取三個候選元素的中位數
int med = medianThree(nums, left, (left + right) / 2, right);
Expand All @@ -79,7 +77,7 @@ int partitionMedian(int nums[], int left, int right) {
return i; // 返回基準數的索引
}

// 中位基準數最佳化-快速排序
/* 快速排序(三數取中值) */
void quickSortMedian(int nums[], int left, int right) {
// 子陣列長度為 1 時終止遞迴
if (left >= right)
Expand All @@ -91,20 +89,25 @@ void quickSortMedian(int nums[], int left, int right) {
quickSortMedian(nums, pivot + 1, right);
}

/* 快速排序類別(尾遞迴最佳化) */
// 快速排序(尾遞迴最佳化)
// 以下為尾遞迴最佳化的快速排序

/* 快速排序(尾遞迴最佳化) */
void quickSortTailCall(int nums[], int left, int right) {
// 子陣列長度為 1 時終止
while (left < right) {
// 哨兵劃分操作
int pivot = partition(nums, left, right);
// 對兩個子陣列中較短的那個執行快速排序
if (pivot - left < right - pivot) {
quickSortTailCall(nums, left, pivot - 1); // 遞迴排序左子陣列
left = pivot + 1; // 剩餘未排序區間為 [pivot + 1, right]
// 遞迴排序左子陣列
quickSortTailCall(nums, left, pivot - 1);
// 剩餘未排序區間為 [pivot + 1, right]
left = pivot + 1;
} else {
quickSortTailCall(nums, pivot + 1, right); // 遞迴排序右子陣列
right = pivot - 1; // 剩餘未排序區間為 [left, pivot - 1]
// 遞迴排序右子陣列
quickSortTailCall(nums, pivot + 1, right);
// 剩餘未排序區間為 [left, pivot - 1]
right = pivot - 1;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fun insert(n0: ListNode?, p: ListNode?) {
fun remove(n0: ListNode?) {
if (n0?.next == null)
return
// n0 -> P -> n1
val p = n0.next
val n1 = p?.next
n0.next = n1
Expand Down Expand Up @@ -78,10 +79,10 @@ fun main() {
printLinkedList(n0)

/* 訪問節點 */
val node: ListNode = access(n0, 3)!!
val node = access(n0, 3)!!
println("鏈結串列中索引 3 處的節點的值 = ${node._val}")

/* 查詢節點 */
val index: Int = find(n0, 2)
val index = find(n0, 2)
println("鏈結串列中值為 2 的節點的索引 = $index")
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fun forLoopRecur(n: Int): Int {
var res = 0
// 遞: 遞迴呼叫
for (i in n downTo 0) {
// 透過“入堆疊操作”模擬“遞”
stack.push(i)
}
// 迴: 返回結果
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ fun constant(n: Int): Int {
/* 線性階 */
fun linear(n: Int): Int {
var count = 0
// 迴圈次數與陣列長度成正比
for (i in 0..<n)
count++
return count
Expand Down Expand Up @@ -55,7 +54,9 @@ fun bubbleSort(nums: IntArray): Int {
for (j in 0..<i) {
if (nums[j] > nums[j + 1]) {
// 交換 nums[j] 與 nums[j + 1]
nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] }
val temp = nums[j]
nums[j] = nums[j + 1]
nums[j + 1] = temp
count += 3 // 元素交換包含 3 個單元操作
}
}
Expand All @@ -66,8 +67,8 @@ fun bubbleSort(nums: IntArray): Int {
/* 指數階(迴圈實現) */
fun exponential(n: Int): Int {
var count = 0
// 細胞每輪一分為二,形成數列 1, 2, 4, 8, ..., 2^(n-1)
var base = 1
// 細胞每輪一分為二,形成數列 1, 2, 4, 8, ..., 2^(n-1)
for (i in 0..<n) {
for (j in 0..<base) {
count++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ fun randomNumbers(n: Int): Array<Int?> {
for (i in 0..<n) {
nums[i] = i + 1
}
val mutableList = nums.toMutableList()
// 隨機打亂陣列元素
mutableList.shuffle()
nums.shuffle()
val res = arrayOfNulls<Int>(n)
for (i in 0..<n) {
res[i] = mutableList[i]
res[i] = nums[i]
}
return res
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ fun climbingStairsDPComp(n: Int): Int {
var a = 1
var b = 2
for (i in 3..n) {
b += a.also { a = b }
val temp = b
b += a
a = temp
}
return b
}
Expand Down
Loading