mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-23 16:47:13 +00:00
fix(C): fix array_hash_map.c and bucket_sort.c (#1272)
* fix: Fix coding error, https://github.com/krahets/hello-algo/discussions/440#discussioncomment-8958379 * fix: Fix issue 1237 https://github.com/krahets/hello-algo/issues/1237 * Update array_hash_map.c * Update bucket_sort.c * Update bucket_sort.c * Update array_hash_map.c * Update bucket_sort.c * Update bucket_sort.c --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -8,66 +8,45 @@
|
||||
|
||||
#define SIZE 10
|
||||
|
||||
/* 比較兩個浮點數的大小 */
|
||||
int compare_float(const void *a, const void *b) {
|
||||
/* 用於 qsort 的比較函式 */
|
||||
int compare(const void *a, const void *b) {
|
||||
float fa = *(const float *)a;
|
||||
float fb = *(const float *)b;
|
||||
return (fa > fb) - (fa < fb);
|
||||
}
|
||||
|
||||
/* 交換兩個浮點數 */
|
||||
void swap(float *a, float *b) {
|
||||
float tmp = *a;
|
||||
*a = *b;
|
||||
*b = tmp;
|
||||
}
|
||||
|
||||
/* 桶排序 */
|
||||
void bucketSort(float nums[], int size) {
|
||||
// 初始化 k = n/2 個桶,預期向每個桶分配 2 個元素
|
||||
int k = size / 2;
|
||||
float **buckets = calloc(k, sizeof(float *));
|
||||
for (int i = 0; i < k; i++) {
|
||||
// 每個桶最多可以分配 size 個元素
|
||||
buckets[i] = calloc(size, sizeof(float));
|
||||
void bucketSort(float nums[], int n) {
|
||||
int k = n / 2; // 初始化 k = n/2 個桶
|
||||
int *sizes = malloc(k * sizeof(int)); // 記錄每個桶的大小
|
||||
float **buckets = malloc(k * sizeof(float *)); // 動態陣列的陣列(桶)
|
||||
|
||||
for (int i = 0; i < k; ++i) {
|
||||
// 為每個桶預分配足夠的空間
|
||||
buckets[i] = (float *)malloc(n * sizeof(float));
|
||||
sizes[i] = 0;
|
||||
}
|
||||
|
||||
// 1. 將陣列元素分配到各個桶中
|
||||
for (int i = 0; i < size; i++) {
|
||||
// 輸入資料範圍為 [0, 1),使用 num * k 對映到索引範圍 [0, k-1]
|
||||
int bucket_idx = nums[i] * k;
|
||||
int j = 0;
|
||||
// 如果桶中有資料且資料小於當前值 nums[i], 要將其放到當前桶的後面,相當於 cpp 中的 push_back
|
||||
while (buckets[bucket_idx][j] > 0 && buckets[bucket_idx][j] < nums[i]) {
|
||||
j++;
|
||||
}
|
||||
float temp = nums[i];
|
||||
while (j < size && buckets[bucket_idx][j] > 0) {
|
||||
swap(&temp, &buckets[bucket_idx][j]);
|
||||
j++;
|
||||
}
|
||||
buckets[bucket_idx][j] = temp;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
int idx = (int)(nums[i] * k);
|
||||
buckets[idx][sizes[idx]++] = nums[i];
|
||||
}
|
||||
|
||||
// 2. 對各個桶執行排序
|
||||
for (int i = 0; i < k; i++) {
|
||||
qsort(buckets[i], size, sizeof(float), compare_float);
|
||||
for (int i = 0; i < k; ++i) {
|
||||
qsort(buckets[i], sizes[i], sizeof(float), compare);
|
||||
}
|
||||
|
||||
// 3. 走訪桶合併結果
|
||||
for (int i = 0, j = 0; j < k; j++) {
|
||||
for (int l = 0; l < size; l++) {
|
||||
if (buckets[j][l] > 0) {
|
||||
nums[i++] = buckets[j][l];
|
||||
}
|
||||
// 3. 合併排序後的桶
|
||||
int idx = 0;
|
||||
for (int i = 0; i < k; ++i) {
|
||||
for (int j = 0; j < sizes[i]; ++j) {
|
||||
nums[idx++] = buckets[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
// 釋放上述分配的記憶體
|
||||
for (int i = 0; i < k; i++) {
|
||||
// 釋放記憶體
|
||||
free(buckets[i]);
|
||||
}
|
||||
free(buckets);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
@@ -79,4 +58,4 @@ int main() {
|
||||
printArrayFloat(nums, SIZE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user