From b824d149cb081a956394c37a8b13e601446ec205 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Wed, 29 Nov 2023 23:14:55 +0800 Subject: [PATCH] fix: Several code bug fixes (#973) * Update counting_sort.c and quick_sort.c * Code bug fixes. --- codes/c/chapter_hashing/hash_map_chaining.c | 2 +- codes/c/chapter_sorting/counting_sort.c | 2 -- codes/c/chapter_sorting/quick_sort.c | 2 +- codes/go/chapter_hashing/hash_map_open_addressing.go | 4 ++-- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/codes/c/chapter_hashing/hash_map_chaining.c b/codes/c/chapter_hashing/hash_map_chaining.c index c04b47614..b68800b13 100644 --- a/codes/c/chapter_hashing/hash_map_chaining.c +++ b/codes/c/chapter_hashing/hash_map_chaining.c @@ -132,7 +132,7 @@ void put(HashMapChaining *hashMap, int key, const char *val) { } cur = cur->next; } - // 若无该 key ,则将键值对添加至尾部 + // 若无该 key ,则将键值对添加至链表头部 Pair *newPair = (Pair *)malloc(sizeof(Pair)); newPair->key = key; strcpy(newPair->val, val); diff --git a/codes/c/chapter_sorting/counting_sort.c b/codes/c/chapter_sorting/counting_sort.c index 54aed970a..01e534eac 100644 --- a/codes/c/chapter_sorting/counting_sort.c +++ b/codes/c/chapter_sorting/counting_sort.c @@ -29,7 +29,6 @@ void countingSortNaive(int nums[], int size) { nums[i] = num; } } - // 4. 释放内存 free(counter); } @@ -65,7 +64,6 @@ void countingSort(int nums[], int size) { } // 使用结果数组 res 覆盖原数组 nums memcpy(nums, res, size * sizeof(int)); - // 5. 释放内存 free(counter); } diff --git a/codes/c/chapter_sorting/quick_sort.c b/codes/c/chapter_sorting/quick_sort.c index 1ecf6f7e8..c8ae9a7f8 100644 --- a/codes/c/chapter_sorting/quick_sort.c +++ b/codes/c/chapter_sorting/quick_sort.c @@ -62,7 +62,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); diff --git a/codes/go/chapter_hashing/hash_map_open_addressing.go b/codes/go/chapter_hashing/hash_map_open_addressing.go index 4db637e6c..5a5dfed1b 100644 --- a/codes/go/chapter_hashing/hash_map_open_addressing.go +++ b/codes/go/chapter_hashing/hash_map_open_addressing.go @@ -51,7 +51,7 @@ func (m *hashMapOpenAddressing) get(key int) string { // 线性探测,从 index 开始向后遍历 for i := 0; i < m.capacity; i++ { // 计算桶索引,越过尾部返回头部 - j := (idx + 1) % m.capacity + j := (idx + i) % m.capacity // 若遇到空桶,说明无此 key ,则返回 null if m.buckets[j] == (pair{}) { return "" @@ -99,7 +99,7 @@ func (m *hashMapOpenAddressing) remove(key int) { // 线性探测,从 index 开始向后遍历 for i := 0; i < m.capacity; i++ { // 计算桶索引,越过尾部返回头部 - j := (idx + 1) % m.capacity + j := (idx + i) % m.capacity // 若遇到空桶,说明无此 key ,则直接返回 if m.buckets[j] == (pair{}) { return