| #include <stdio.h> |
| #include <stdlib.h> |
| #include <math.h> |
| |
| #include <sys/times.h> |
| #include <stdbool.h> |
| |
| |
| #define SIZE 16 |
| |
| |
| |
| struct size_and_data |
| { |
| int size; |
| int *data; |
| }; |
| |
| struct bin_info |
| { |
| int size; |
| int *data; |
| }; |
| |
| |
| |
| |
| |
| |
| |
| int *allocate(int size) |
| { |
| int *space; |
| space = (int *)calloc(size, sizeof(int)); |
| if (space == NULL) |
| { |
| perror("Problem allocate memory.\n"); |
| exit(EXIT_SUCCESS); |
| } |
| return space; |
| } |
| |
| |
| |
| |
| |
| |
| |
| void produce_random_data(struct size_and_data array) |
| { |
| srand(1); |
| for (int i = 0; i < array.size; i++) |
| { |
| array.data[i] = rand() % 1000; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| void print_data(struct size_and_data array, char * txt) |
| { |
| FILE *fp; |
| |
| char num_str[4] = {}; |
| |
| fp = fopen(txt, "w"); |
| if (fp == NULL) |
| { |
| perror("Fail to open txt"); |
| exit(EXIT_SUCCESS); |
| } |
| |
| for (int i = 0; i < array.size; i++) |
| { |
| sprintf(num_str, "%d", array.data[i]); |
| fwrite(num_str, sizeof(num_str), 1, fp); |
| fputc('\n', fp); |
| } |
| |
| fclose(fp); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| void split_data(struct size_and_data array, struct bin_info bins[]) |
| { |
| for (int i = 0; i < array.size; i++) |
| { |
| int number = array.data[i]; |
| if (number < 250) |
| { |
| bins[0].data[bins[0].size++] = number; |
| } |
| else if (number < 500) |
| { |
| bins[1].data[bins[1].size++] = number; |
| } |
| else if (number < 750) |
| { |
| bins[2].data[bins[2].size++] = number; |
| } |
| else |
| { |
| bins[3].data[bins[3].size++] = number; |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| void insertion(struct bin_info bin) |
| { |
| for (int i = 1; i < bin.size; i++) |
| { |
| for (int j = i; j > 0; j--) |
| { |
| if (bin.data[j - 1] > bin.data[j]) |
| { |
| int temp; |
| temp = bin.data[j]; |
| bin.data[j] = bin.data[j - 1]; |
| bin.data[j - 1] = temp; |
| } |
| else |
| { |
| break; |
| } |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| void move_data(struct size_and_data array, struct bin_info bins[]) |
| { |
| for (int bin = 0; bin < 4; bin++) |
| { |
| for (int i = 0; i < bins[bin].size; i++) |
| { |
| |
| *array.data++ = bins[bin].data[i]; |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| bool is_sorted(struct size_and_data array) |
| { |
| bool sorted = true; |
| for (int i = 0; i < array.size - 1; i++) |
| { |
| if (array.data[i] > array.data[i + 1]) |
| sorted = false; |
| } |
| return sorted; |
| } |
| |
| |
| |
| |
| |
| int main(int argc, char *argv[]) |
| { |
| |
| struct size_and_data the_array; |
| struct bin_info bins[4]; |
| |
| |
| if (argc < 2) |
| { |
| the_array.size = SIZE; |
| } |
| else |
| { |
| the_array.size = pow(2, atoi(argv[1])); |
| } |
| |
| |
| the_array.data = allocate(the_array.size); |
| for (int i = 0; i < 4; i++) |
| { |
| bins[i].size = 0; |
| bins[i].data = allocate(the_array.size); |
| } |
| |
| |
| produce_random_data(the_array); |
| print_data(the_array, "data.txt"); |
| |
| |
| struct tms start_times, finish_times; |
| time_t start_clock, finish_clock; |
| |
| start_clock = times(NULL); |
| times(&start_times); |
| printf("start time in clock ticks: %ld\n", start_times.tms_utime); |
| |
| |
| split_data(the_array, bins); |
| |
| |
| for (int i = 0; i < 4; i++) |
| { |
| insertion(bins[i]); |
| } |
| |
| |
| move_data(the_array, bins); |
| |
| |
| times(&finish_times); |
| finish_clock = times(NULL); |
| printf("finish time in clock ticks: %ld\n", finish_times.tms_utime); |
| printf("Total elapsed time in seconds: %ld\n", finish_clock - start_clock); |
| |
| |
| print_data(the_array, "output.txt"); |
| |
| |
| printf(is_sorted(the_array) ? "sorted\n" : "not sorted\n"); |
| |
| |
| free(the_array.data); |
| for (int i = 0; i < 4; i++) |
| { |
| free(bins[i].data); |
| } |
| |
| exit(EXIT_SUCCESS); |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具