接上文,本篇向大家简单展示一下外排序的实现。先三连后看才是好习惯!!!
在我们刚接触数据结构的时间里,我们只需要对外排序简单了解一下即可,重点要掌握的还是前面我们介绍的比较排序和非比较排序里的计数排序!
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int Getmidi(int* a, int left, int right)
{
//数组有序时,midi就是key,这样就解决了hoare版本的最坏情况
int mid = (left + right) / 2;
//比较begin、midi和end对应元素的大小,选择位于中间大小的作为a[key]
if (a[left] < a[mid])
{
if (a[mid] < a[right])
return mid;
else if (a[left] > a[right])
return left;
else
return right;
}
else //a[left] > a[mid]
{
if (a[mid] > a[right])
return mid;
else if (a[right] < a[left])
return right;
else
return left;
}
}
void Quicksort(int* a, int begin, int end)
{
if (begin >= end)
{
return;
}
int left = begin;
int right = end;
//小区间走插入排序
if (right - left + 1 < 15)
{
InsertSort(a + left, right - left + 1);
//a+left指向小区间的起始位置
//right-left+1是小区间长度
}
//三数取中
int midi = Getmidi(a, left, right);
Swap(&a[left], &a[midi]);
int key = left;
while (left < right)
{
while (left < right && a[right] >= a[key])
{
right--;
}
while (left < right && a[left] <= a[key])
{
left++;
}
Swap(&a[right], &a[left]);
}
Swap(&a[left], &a[key]);
key = left;
Quicksort(a, begin, key - 1);
Quicksort(a, key + 1, end);
}
void _MergeFile(const char* file1, const char* file2, const char* mfile)
{
FILE* fout1 = fopen(file1, "r");
if (fout1 == NULL)
{
printf("文件打开失败\n");
exit(-1);
}
FILE* fout2 = fopen(file2, "r");
if (fout2 == NULL)
{
printf("文件打开失败\n");
exit(-1);
}
FILE* fin = fopen(mfile, "w");
if (fin == NULL)
{
printf("文件打开失败\n");
exit(-1);
}
int num1, num2;
int ret1 = fscanf(fout1, "%d\n", &num1);
int ret2 = fscanf(fout2, "%d\n", &num2);
while (ret1 != EOF && ret2 != EOF)
{
if (num1 < num2)
{
fprintf(fin, "%d\n", num1);
ret1 = fscanf(fout1, "%d\n", &num1);
}
else
{
fprintf(fin, "%d\n", num2);
ret2 = fscanf(fout2, "%d\n", &num2);
}
}
while (fscanf(fout1, "%d\n", &num1))
{
fprintf(fin, "%d\n", num1);
}
while (fscanf(fout2, "%d\n", &num2))
{
fprintf(fin, "%d\n", num2);
}
fclose(fout1);
fclose(fout2);
fclose(fin);
}
void MergeSortFile(const char* file)
{
FILE* fout = fopen(file, "r");
if (fout == NULL)
{
printf("文件打开失败\n");
exit(-1);
}
//大文件平均分割成n份,保证每份的大小都可以加载到内存里
//把这些小文件加载到内存中先进行快排,再写会小文件,最后进行归并
int n = 10;
int a[10];
int i = 0;
int num = 0;
char subfile[20];
int filei = 0;
memset(a, 0, sizeof(int) * n);
//从文件里读取数据
//"%d\n"表示读一个整形,换一行继续读数据
//这里就按文件里数据存储形式进行修改即可
//如"%d "表示读一个整形,空一格继续读下一个数据
while (fscanf(fout, "%d\n", &num) != EOF)
{
if (i < n-1)
a[i++] = num;
else
{
a[i] = num;
Quicksort(a, 0, n - 1);
sprintf(subfile, "sub\\sub_sort%d", filei++);//每个小文件都是有序的
FILE* fin = fopen(subfile, "w");
if (fin == NULL)
{
printf("文件打开失败\n");
exit(-1);
}
for (int i = 0; i < n; i++)
{
fprintf(fin, "%d\n", a[i]);
}
fclose(fin);
i = 0;//读下一组数据要从i=0重新开始
memset(a, 0, sizeof(int) * n);
}
}
//小文件相互归并
char mfile[100] = "12.txt";
char file1[100] = "1";
char file2[100] = "2";
for (int i = 1; i < n; i++)
{
sprintf(file2, "sub\\sub_sort%d.txt", i);
//读取file1和file2,归并出mfile
_MergeFile(file1, file2, mfile);
strcpy(file1, mfile);
sprintf(mfile, "%s%d.txt", mfile, i + 1);
}
fclose(fout);
}