mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 06:07:20 +08:00 
			
		
		
		
	fix: Several code bug fixes (#973)
* Update counting_sort.c and quick_sort.c * Code bug fixes.
This commit is contained in:
		@ -132,7 +132,7 @@ void put(HashMapChaining *hashMap, int key, const char *val) {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        cur = cur->next;
 | 
					        cur = cur->next;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    // 若无该 key ,则将键值对添加至尾部
 | 
					    // 若无该 key ,则将键值对添加至链表头部
 | 
				
			||||||
    Pair *newPair = (Pair *)malloc(sizeof(Pair));
 | 
					    Pair *newPair = (Pair *)malloc(sizeof(Pair));
 | 
				
			||||||
    newPair->key = key;
 | 
					    newPair->key = key;
 | 
				
			||||||
    strcpy(newPair->val, val);
 | 
					    strcpy(newPair->val, val);
 | 
				
			||||||
 | 
				
			|||||||
@ -29,7 +29,6 @@ void countingSortNaive(int nums[], int size) {
 | 
				
			|||||||
            nums[i] = num;
 | 
					            nums[i] = num;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
    // 4. 释放内存
 | 
					    // 4. 释放内存
 | 
				
			||||||
    free(counter);
 | 
					    free(counter);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -65,7 +64,6 @@ 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(counter);
 | 
					    free(counter);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -62,7 +62,7 @@ int medianThree(int nums[], int left, int mid, int right) {
 | 
				
			|||||||
        return right;
 | 
					        return right;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 哨兵划分(三数取中值)
 | 
					/* 哨兵划分(三数取中值) */ 
 | 
				
			||||||
int partitionMedian(int nums[], int left, int right) {
 | 
					int partitionMedian(int nums[], int left, int right) {
 | 
				
			||||||
    // 选取三个候选元素的中位数
 | 
					    // 选取三个候选元素的中位数
 | 
				
			||||||
    int med = medianThree(nums, left, (left + right) / 2, right);
 | 
					    int med = medianThree(nums, left, (left + right) / 2, right);
 | 
				
			||||||
 | 
				
			|||||||
@ -51,7 +51,7 @@ func (m *hashMapOpenAddressing) get(key int) string {
 | 
				
			|||||||
	// 线性探测,从 index 开始向后遍历
 | 
						// 线性探测,从 index 开始向后遍历
 | 
				
			||||||
	for i := 0; i < m.capacity; i++ {
 | 
						for i := 0; i < m.capacity; i++ {
 | 
				
			||||||
		// 计算桶索引,越过尾部返回头部
 | 
							// 计算桶索引,越过尾部返回头部
 | 
				
			||||||
		j := (idx + 1) % m.capacity
 | 
							j := (idx + i) % m.capacity
 | 
				
			||||||
		// 若遇到空桶,说明无此 key ,则返回 null
 | 
							// 若遇到空桶,说明无此 key ,则返回 null
 | 
				
			||||||
		if m.buckets[j] == (pair{}) {
 | 
							if m.buckets[j] == (pair{}) {
 | 
				
			||||||
			return ""
 | 
								return ""
 | 
				
			||||||
@ -99,7 +99,7 @@ func (m *hashMapOpenAddressing) remove(key int) {
 | 
				
			|||||||
	// 线性探测,从 index 开始向后遍历
 | 
						// 线性探测,从 index 开始向后遍历
 | 
				
			||||||
	for i := 0; i < m.capacity; i++ {
 | 
						for i := 0; i < m.capacity; i++ {
 | 
				
			||||||
		// 计算桶索引,越过尾部返回头部
 | 
							// 计算桶索引,越过尾部返回头部
 | 
				
			||||||
		j := (idx + 1) % m.capacity
 | 
							j := (idx + i) % m.capacity
 | 
				
			||||||
		// 若遇到空桶,说明无此 key ,则直接返回
 | 
							// 若遇到空桶,说明无此 key ,则直接返回
 | 
				
			||||||
		if m.buckets[j] == (pair{}) {
 | 
							if m.buckets[j] == (pair{}) {
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user