C语言写在磁盘中进行文件内数据的排序
时间: 2024-02-20 17:58:16 浏览: 131
c语言中的排序
下面是使用多路归并排序算法在磁盘中进行文件内数据的排序的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_FILE_NUM 10 // 最多分成10个小文件
#define MAX_RECORD_NUM 10000 // 每个小文件最多10000个记录
// 定义记录结构体
typedef struct Record {
int key;
char value[256];
} Record;
// 定义归并排序函数
void mergeSort(Record* arr, int left, int right) {
if (left >= right) {
return;
}
int mid = (left + right) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
Record* tmp = (Record*)malloc((right - left + 1) * sizeof(Record));
int i = left, j = mid + 1, k = 0;
while (i <= mid && j <= right) {
if (arr[i].key <= arr[j].key) {
tmp[k++] = arr[i++];
} else {
tmp[k++] = arr[j++];
}
}
while (i <= mid) {
tmp[k++] = arr[i++];
}
while (j <= right) {
tmp[k++] = arr[j++];
}
for (i = left, k = 0; i <= right; i++, k++) {
arr[i] = tmp[k];
}
free(tmp);
}
// 定义归并函数
void mergeFiles(char* file_prefix, int file_num, char* output_file) {
char** file_names = (char**)malloc(file_num * sizeof(char*));
int i;
for (i = 0; i < file_num; i++) {
file_names[i] = (char*)malloc(100 * sizeof(char));
sprintf(file_names[i], "%s%d", file_prefix, i);
}
FILE** files = (FILE**)malloc(file_num * sizeof(FILE*));
for (i = 0; i < file_num; i++) {
files[i] = fopen(file_names[i], "r");
}
FILE* out_file = fopen(output_file, "w");
Record** records = (Record**)malloc(file_num * sizeof(Record*));
for (i = 0; i < file_num; i++) {
records[i] = (Record*)malloc(MAX_RECORD_NUM * sizeof(Record));
fread(records[i], sizeof(Record), MAX_RECORD_NUM, files[i]);
}
int* index = (int*)malloc(file_num * sizeof(int));
for (i = 0; i < file_num; i++) {
index[i] = 0;
}
int count = 0;
while (count < file_num * MAX_RECORD_NUM) {
int min_index = -1;
Record min_record;
for (i = 0; i < file_num; i++) {
if (index[i] < MAX_RECORD_NUM) {
if (min_index == -1 || records[i][index[i]].key < min_record.key) {
min_index = i;
min_record = records[i][index[i]];
}
}
}
fwrite(&min_record, sizeof(Record), 1, out_file);
index[min_index]++;
count++;
}
for (i = 0; i < file_num; i++) {
fclose(files[i]);
remove(file_names[i]);
}
fclose(out_file);
for (i = 0; i < file_num; i++) {
free(records[i]);
free(file_names[i]);
}
free(records);
free(files);
free(file_names);
free(index);
}
// 定义排序函数
void sortFile(char* input_file, char* output_file) {
FILE* in_file = fopen(input_file, "r");
int block_size = MAX_RECORD_NUM * sizeof(Record);
char* buffer = (char*)malloc(block_size);
int file_count = 0;
while (!feof(in_file)) {
int read_count = fread(buffer, sizeof(char), block_size, in_file);
if (read_count > 0) {
int record_count = read_count / sizeof(Record);
Record* records = (Record*)buffer;
mergeSort(records, 0, record_count - 1);
char file_name[100];
sprintf(file_name, "tmp%d", file_count++);
FILE* out_file = fopen(file_name, "w");
fwrite(records, sizeof(Record), record_count, out_file);
fclose(out_file);
}
}
fclose(in_file);
mergeFiles("tmp", file_count, output_file);
free(buffer);
}
// 测试代码
int main() {
sortFile("input.txt", "output.txt");
return 0;
}
```
上述代码中,我们首先定义了记录结构体,包括关键字和值。然后定义了归并排序函数和归并函数,用于对小文件进行排序和归并。最后定义了排序函数,该函数将输入文件分成多个小文件,并对每个小文件进行排序,然后将所有小文件进行归并得到有序的输出文件。
在测试代码中,我们调用了排序函数对输入文件进行排序,并将结果输出到输出文件中。其中输入文件为 "input.txt",输出文件为 "output.txt"。
阅读全文