|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 houqing1999 于 2023-7-7 21:44 编辑
完整代码如下,想问一下
1为什么add_item 要最后新添加一个元素newArray[count] = item;
2第二个函数里找最大值最小值的for循环初始为啥i=1,这样少比较一个元素吧,
3void bucket_sort(int array[], int length)里面的61行为啥是if (b.count < 0)74行的条件判断为啥是size > 0
- #define _CRT_SECURE_NO_WARNINGS 1
- #include <stdio.h>
- #include <stdlib.h>
- int* add_item(int* array, int count, int item);
- void bucket_sort(int array[], int length);
- struct Bucket
- {
- int* data;
- int count;
- };
- int* add_item(int* array, int count, int item)
- {
- int* newArray = (int*)malloc(sizeof(int) * (count + 1));
- int i;
- for (i = 0; i < count; i++)
- {
- newArray[i] = array[i];
- }
- newArray[count] = item;
- return newArray;
- }
- void bucket_sort(int array[], int length)
- {
- int min = array[0];
- int max = array[0];
- int i, j, k, cap, size;
- struct Bucket* bucket = NULL;
- struct Bucket b;
- for (i = 1; i < length; i++)
- {
- if (array[i] > max)
- {
- max = array[i];
- }
- if (array[i] < min)
- {
- min = array[i];
- }
- }
- cap = max - min + 1;
- bucket = (struct Bucket*)malloc(sizeof(struct Bucket) * cap);
- for (i = 0; i < cap; i++)
- {
- bucket[i] = *(struct Bucket*)malloc(sizeof(struct Bucket));
- }
- for (i = 0; i < length; i++)
- {
- b = bucket[array[i] - min];
- if (b.count < 0)
- {
- b.count = 0;
- }
- bucket[array[i] - min].data = add_item(b.data, b.count++, array[i]);
- bucket[array[i] - min].count = b.count;
- }
- for (i = 0, k = 0; i < cap; i++)
- {
- size = bucket[i].count;
- if (size > 0)
- {
- for (j = 0; j < size; j++)
- {
- array[k] = bucket[i].data[j];
- k++;
- }
- }
- }
- }
- int main(void)
- {
- int array[] = { 73, 108, 111, 118, 101, 70, 105, 115, 104, 67, 46, 99, 111, 109 };
- int i, length;
- length = sizeof(array) / sizeof(array[0]);
- bucket_sort(array, length);
- printf("排序后的结果是:");
- for (i = 0; i < length; i++)
- {
- printf("%d ", array[i]);
- }
- putchar('\n');
- return 0;
- }
复制代码
代码详情索引https://fishc.com.cn/forum.php?m ... 040&pid=6266730 |
|