mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18: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;
 | 
			
		||||
    }
 | 
			
		||||
    // 若无该 key ,则将键值对添加至尾部
 | 
			
		||||
    // 若无该 key ,则将键值对添加至链表头部
 | 
			
		||||
    Pair *newPair = (Pair *)malloc(sizeof(Pair));
 | 
			
		||||
    newPair->key = key;
 | 
			
		||||
    strcpy(newPair->val, val);
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user