多线程排序-v0

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// #include <time.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;
};
/*
@name :申请内存
@input :申请内存的尺寸大小
@output:以申请内存的首地址
@func :Allocate space for the data or a bin.
*/
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;
}
/*
@name :生成随机数据
@input :存储数据的结构体
@output:none
@func :Fill the array with random data.
*/
void produce_random_data(struct size_and_data array)
{
srand(1);
for (int i = 0; i < array.size; i++)
{
array.data[i] = rand() % 1000;
}
}
/*
@name :生成随机数据
@input :存储数据的结构体
@output:none
@func :Fill the array with random data.
*/
void print_data(struct size_and_data array, char * txt)
{
FILE *fp;
//char *txt = "data.txt";
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);
}
/*
@name :分配数据
@input :原始数据的结构体,缓存数据的结构体
@output:none
@func :分配原始数据到缓存数据
*/
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;
}
}
}
/*
@name :
@input :
@output:
@func :排序算法
*/
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;
}
}
}
}
/*
@name :
@input :
@output:
@func :把数据从缓存区搬到原始数据区
*/
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];
}
}
}
/*
@name :
@input :
@output:
@func :判断是否正确排序
*/
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);
}
posted @   steve的miao  阅读(34)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示