紙藤長方形的程式設計

  • 作者:由 匿名使用者 發表于 書法
  • 2021-11-08

紙藤長方形的程式設計物理公司的 2016-03-08

#include

#include

voidMerge(int*a,intlow,intmid,inthigh){

inti=low,j=mid+1,k=0;

int*temp=(int*)malloc((high-low+1)*sizeof(int));

while(i<=mid&&j<=high)

a[i]<=a[j]?(temp[k++]=a[i++]):(temp[k++]=a[j++]);

while(i<=mid)

temp[k++]=a[i++];

while(j<=high)

temp[k++]=a[j++];

memcpy(a+low,temp,(high-low+1)*sizeof(int));

free(temp);

}

voidMergeSort(int*a,intn){

intlength;

for(length=1;length

inti;

for(i=0;i+2*length-1<=n-1;i+=2*length)

Merge(a,i,i+length-1,i+2*length-1);

if(i+length<=n-1)//尚有兩個子檔案,其中後一個長度小於length

Merge(a,i,i+length-1,n-1);

}

}

intmain(){

intn;

cin>>n;

int*data=new

int[n];

if(!data)

exit(1);

intk=n;

while(k——){

cin>>data[n-k-1];

}

clock_ts=clock();

MergeSort(data,n);

clock_te=clock();

k=n;

while(k——){

cout<

}

cout<

cout<<“thealgrothemused”<

deletedata;

return0;

}

Top