📄 bing_sort.c
字号:
/*此程序是采用了在归并排序的思想来排序
其中将两个无序的文件读出,采用shell排序的方法来排序
然后采用归并排序来将这两个文件的数据来排序的目的。*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define NUM 50
void dis_sort(int *,int *,int *,int ,int );
void shellsort(int *,int );
void main(void)
{
int source1[NUM];
int source2[NUM];
int *dis;
int i=NUM,j=NUM;
int k=0;
FILE *fp;
dis=(int *)malloc(sizeof(int)*(i+j));
/*******************************************/
fp=fopen("e:\\data1.dat","rb");/*读取文件data1.dat*/
if(fp==NULL)
{
printf("the file cannot be open.\n");
exit(0);
}
fread(source1,sizeof(int),NUM,fp);
fclose(fp);
i=NUM;
shellsort(source1,i);/*shell排序*/
/********************************************/
fp=fopen("e:\\data2.dat","rb"); /*读取文件data2.dat*/
if(fp==NULL)
{
printf("the file cannot be open.\n");
exit(0);
}
fread(source2,sizeof(int),NUM,fp);
fclose(fp);
j=NUM;
shellsort(source2,j);/*shell排序*/
/********************************************/
/********************************************/
dis_sort(source1,source2,dis,i,j); /*调用归并函数*/
i=NUM;
j=NUM;
for(k=0;k<i+j;k++)
{
printf("%d\t",dis[k]);
}
}
void dis_sort(int *one,int *two,int *all,int i,int j) /*归并排序函数*/
{
int a=0;
int b=0;
int d=0;
while(1)
{
if(one[a]<two[b])
{
all[d]=one[a];
d++;
a++;
}
else
{
all[d]=two[b];
d++;
b++;
}
if((a>=i)&&(b>=j))
break;
else if((a>=i)&&(b<j))
for(;b<j;b++)
{
all[d]=two[b];
d++;
}
else if((a<i)&&(b>=j))
for(;a<i;a++)
{
all[d]=one[a];
d++;
}
else
continue;
}
}
void shellsort(int *list,int index) /*shell排序函数*/
{
int j;
int change;
int temp;
int length;
int process;
length=index/2;
while(length!=0)
{
for(j=length;j<index;j++)
{
change=0;
temp=list[j];
process=j-length;
while(temp<list[process]&&process>=0&&j<=index)
{
list[process+length]=list[process];
process=process-length;
change=1;
}
list[process+length]=temp;
}
length=length/2;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -