Skip to content

Commit

Permalink
fix: Several code bug fixes (krahets#973)
Browse files Browse the repository at this point in the history
* Update counting_sort.c and quick_sort.c

* Code bug fixes.
  • Loading branch information
krahets authored Nov 29, 2023
1 parent 56b20ef commit b824d14
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 6 deletions.
2 changes: 1 addition & 1 deletion codes/c/chapter_hashing/hash_map_chaining.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions codes/c/chapter_sorting/counting_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ void countingSortNaive(int nums[], int size) {
nums[i] = num;
}
}

// 4. 释放内存
free(counter);
}
Expand Down Expand Up @@ -65,7 +64,6 @@ void countingSort(int nums[], int size) {
}
// 使用结果数组 res 覆盖原数组 nums
memcpy(nums, res, size * sizeof(int));

// 5. 释放内存
free(counter);
}
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_sorting/quick_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions codes/go/chapter_hashing/hash_map_open_addressing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b824d14

Please sign in to comment.