主题:[原创]归并排序原创
[em1][em12]
//This program is to sort the random numbers
//头文件
#include <iostream.h> //输入输出文件
#include <stdlib.h> //随机函数文件
#include <time.h> //时间文件
#include <conio.h> //使用函数文件
#include <iomanip.h> //setw使用文件
#include <fstream.h> //输出函数文件
//using namespace std;
//#define RAND_MAX 100000000//扩大随机数范围
#define N 1000
//函数文件
long a[N];// extern variable
void merge(int low,int mid,int high)
{
int h,i,j,k;
int b[N];
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}//while
if(h>mid)
for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
else
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i++;
}//if
for(k=low;k<=high;k++)
a[k]=b[k];
}//merge
void mergesort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(low,mid);
mergesort(mid+1,high);
merge(low,mid,high);
}
}//mergesort
//主函数文件
void main(void)
{
int i;
ofstream Table;
time_t start,finish;
time(&start);
// randomize();
// double seed=(double)time(NULL)
// cout.self(ios::fill(ios::left));
// cout.precision(8);
for(i=0;i<N;i++)
{
// double rand=rand_01_One(seed);
a[i]=(int)((rand()/32768.0)*100000000);
/* if((a[i]<1e+8)||(a[i]>1e+9))
do{
a[i]=rand();
}while((a[i]>=1e+8)&&(a[i]<1e+9)); */
}
cout<<"排序前:"<<endl;
for(int p=0;p<N;p++)
{
cout.fill('0');
Table.fill('0');
cout<<setw(8)<<a[p]<<" ";
Table<<setw(8)<<a[p]<<" ";
if((p+7)%6==0)
cout<<endl;
}
cout<<endl;
cout<<"开始时间为:"<<start<<endl;
mergesort(0,N-1);
time(&finish);
Table.open("e:\\result.txt");
if(!Table.is_open())
Table<<"open file failed!"<<endl;
else
{
cout<<"排序后:"<<endl;
for(int p=0;p<N;p++)
{
cout.fill('0');
Table.fill('0');
cout<<setw(8)<<a[p]<<" ";
Table<<setw(8)<<a[p]<<" ";
if((p+7)%6==0)
cout<<endl;
}
}
cout<<endl<<"结束时间为:"<<finish<<endl;
cout<<"排序时间为"<<difftime(finish,start);
Table.close();
cout<<endl;
// cout<<"RAND_MAX="<<RAND_MAX<<endl;
getch();
}//end main
[em20]
//This program is to sort the random numbers
//头文件
#include <iostream.h> //输入输出文件
#include <stdlib.h> //随机函数文件
#include <time.h> //时间文件
#include <conio.h> //使用函数文件
#include <iomanip.h> //setw使用文件
#include <fstream.h> //输出函数文件
//using namespace std;
//#define RAND_MAX 100000000//扩大随机数范围
#define N 1000
//函数文件
long a[N];// extern variable
void merge(int low,int mid,int high)
{
int h,i,j,k;
int b[N];
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}//while
if(h>mid)
for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
else
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i++;
}//if
for(k=low;k<=high;k++)
a[k]=b[k];
}//merge
void mergesort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(low,mid);
mergesort(mid+1,high);
merge(low,mid,high);
}
}//mergesort
//主函数文件
void main(void)
{
int i;
ofstream Table;
time_t start,finish;
time(&start);
// randomize();
// double seed=(double)time(NULL)
// cout.self(ios::fill(ios::left));
// cout.precision(8);
for(i=0;i<N;i++)
{
// double rand=rand_01_One(seed);
a[i]=(int)((rand()/32768.0)*100000000);
/* if((a[i]<1e+8)||(a[i]>1e+9))
do{
a[i]=rand();
}while((a[i]>=1e+8)&&(a[i]<1e+9)); */
}
cout<<"排序前:"<<endl;
for(int p=0;p<N;p++)
{
cout.fill('0');
Table.fill('0');
cout<<setw(8)<<a[p]<<" ";
Table<<setw(8)<<a[p]<<" ";
if((p+7)%6==0)
cout<<endl;
}
cout<<endl;
cout<<"开始时间为:"<<start<<endl;
mergesort(0,N-1);
time(&finish);
Table.open("e:\\result.txt");
if(!Table.is_open())
Table<<"open file failed!"<<endl;
else
{
cout<<"排序后:"<<endl;
for(int p=0;p<N;p++)
{
cout.fill('0');
Table.fill('0');
cout<<setw(8)<<a[p]<<" ";
Table<<setw(8)<<a[p]<<" ";
if((p+7)%6==0)
cout<<endl;
}
}
cout<<endl<<"结束时间为:"<<finish<<endl;
cout<<"排序时间为"<<difftime(finish,start);
Table.close();
cout<<endl;
// cout<<"RAND_MAX="<<RAND_MAX<<endl;
getch();
}//end main
[em20]