归并排序法修改
#include <iostream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <conio.h>
#include <time.h>
const int N=65536*2;
void BottomUpSort(short[],int);
void Merg(short[],int,int,int);
void main()
{
short A[N+1];
srand(time(NULL));
for(int i=1;i<=N;i++)A[i]=rand()%1000;
time_t t1,t2; struct tm pt;
time(&t1);pt=gmtime(&t1);
cout<<setfill(‘0’);
cout<<“排序开始时间:”;
cout<<setw(2)<<(pt->tm_hour+8)%24<<":"<<setw(2)<tm_min<<":"<<setw(2)<tm_sec<<endl;
cout<<“MergeSort排序中”<<flush;
BottomUpSort(A,N);
time(&t2);pt=gmtime(&t2);
cout<<endl<<“排序结束时间:”;
cout<<setw(2)<<(pt->tm_hour+8)%24<<":"<<setw(2)<tm_min<<":"<<setw(2)<tm_sec<<endl;
cout<<“排序耗费时间=”<<t2-t1<<endl;//
getch();
}
void BottomUpSort(short A[],int n)
{
int i,t=1,s;
while(t<n){
cout<<"."<<flush;
s=t;t=2s;i=0;
……………………………………
// *******************************************************
// * 工 程 名:104合并.dsp *
// * 程 序 名:104合并.cpp *
// * 主要功能:合并排序法时间 *
// * 编制时间:2019年11月21日 *
// ********************************************************
#include
#include <stdlib.h>
#include
#include <conio.h>
#include <time.h>
#define N 655362
using namespace std;
void merge(int A[],int low,int mid,int high)
{
int b[N+1];
int s=low,t=mid+1,k=low;
while(s<=mid&&t<=high)
{
if(A[s]<=A[t])
{
b[k]=A[s];
s++;
}
else
{
b[k]=A[t];
t++;
}
k++;
}
if(s==mid+1)
for(;t<=high;k++,t++)
b[k]=A[t];
else
for(;s<=mid;k++,s++)
b[k]=A[s];
for(int i=low;i<=high;i++)
A[i]=b[i];
}
void Bottomupsort(int A[],int n)
{
int i,t=1,s;
while(t<n)
{
s=t;
t=2s;
i=0;
while((i+t)<=n)
{
merge(A,i,i+s-1,i+t-1);
i=i+t;
}
if(i+s-1<n)
merge(A,i,i+s-1,n);
}
return;
}
int main()
{
int A[N+1];
srand(time(NULL));
for(int i=1;i<=N;i++)A[i]=rand()%1000;
time_t t1,t2; struct tm *pt;
time(&t1);pt=gmtime(&t1);
cout<<setfill(‘0’);
cout<<“排序开始时间:”;
cout<<setw(2)<<(pt->tm_hour+8)%24<<":"<<setw(2)<tm_min<<":"<<setw(2)<tm_sec<<endl;
cout<<“MergeSort排序中”<<flush;
Bottomupsort(A,N);
time(&t2);pt=gmtime(&t2);
cout<<endl<<“排序结束时间:”;
cout<<setw(2)<<(pt->tm_hour+8)%24<<":"<<setw(2)<<pt->tm_min<<":"<<setw(2)<<pt->tm_sec<<endl;
cout<<"排序耗费时间="<<t2-t1<<endl;//
getch();
}