fix(lang: c, chapters: searching, sorting): Some serious errors (#1531)
Some checks failed
C / build (Release, clang, clang++, ubuntu-latest) (push) Failing after 31s
C / build (Release, gcc, g++, ubuntu-latest) (push) Failing after 12s
C / build (Release, cl, cl, windows-latest) (push) Has been cancelled

* fix: correct hash table insertion

* Use a pointer to a pointer instead of a single pointer in the insert function, ensuring proper updates to the hash table
* Bug fixes: This fixes issues with empty results.

* fix: correct issues in radix sort

* Replaced loop-based array copy with memcpy to ensure efficient memory copying in countingSortDigit
* Fixed a bug in radixSort where the maximum value search loop skipped the last element
* Bug fix: Ensures all elements are checked when determining the maximum value in the array

* perf: improve memory management by adding explicit memory release

* revert: Revert to using the old loop override array method and drop specific api's like memcpy.
This commit is contained in:
Fleey 2024-11-10 02:00:53 +08:00 committed by GitHub
parent 73eab4c0ec
commit 6a74972080
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 11 additions and 6 deletions

View file

@ -37,12 +37,12 @@ HashTable *find(HashTable *h, int key) {
} }
/* 哈希表元素插入 */ /* 哈希表元素插入 */
void insert(HashTable *h, int key, int val) { void insert(HashTable **h, int key, int val) {
HashTable *t = find(h, key); HashTable *t = find(*h, key);
if (t == NULL) { if (t == NULL) {
HashTable *tmp = malloc(sizeof(HashTable)); HashTable *tmp = malloc(sizeof(HashTable));
tmp->key = key, tmp->val = val; tmp->key = key, tmp->val = val;
HASH_ADD_INT(h, key, tmp); HASH_ADD_INT(*h, key, tmp);
} else { } else {
t->val = val; t->val = val;
} }
@ -59,7 +59,7 @@ int *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {
*returnSize = 2; *returnSize = 2;
return res; return res;
} }
insert(hashtable, nums[i], i); insert(&hashtable, nums[i], i);
} }
*returnSize = 0; *returnSize = 0;
return NULL; return NULL;
@ -83,4 +83,4 @@ int main() {
printArray(res, returnSize); printArray(res, returnSize);
return 0; return 0;
} }

View file

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

View file

@ -16,6 +16,7 @@ int digit(int num, int exp) {
void countingSortDigit(int nums[], int size, int exp) { void countingSortDigit(int nums[], int size, int exp) {
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶数组 // 十进制的位范围为 0~9 ,因此需要长度为 10 的桶数组
int *counter = (int *)malloc((sizeof(int) * 10)); int *counter = (int *)malloc((sizeof(int) * 10));
memset(counter, 0, sizeof(int) * 10); // 初始化为 0 以支持后续内存释放
// 统计 0~9 各数字的出现次数 // 统计 0~9 各数字的出现次数
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
// 获取 nums[i] 第 k 位,记为 d // 获取 nums[i] 第 k 位,记为 d
@ -39,13 +40,16 @@ void countingSortDigit(int nums[], int size, int exp) {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
nums[i] = res[i]; nums[i] = res[i];
} }
// 释放内存
free(res);
free(counter);
} }
/* 基数排序 */ /* 基数排序 */
void radixSort(int nums[], int size) { void radixSort(int nums[], int size) {
// 获取数组的最大元素,用于判断最大位数 // 获取数组的最大元素,用于判断最大位数
int max = INT32_MIN; int max = INT32_MIN;
for (size_t i = 0; i < size - 1; i++) { for (int i = 0; i < size; i++) {
if (nums[i] > max) { if (nums[i] > max) {
max = nums[i]; max = nums[i];
} }