void merge_pass( ElementType list[], ElementType sorted[], int N, int length ){
int h1=0,e1=length-1,h2=length,e2=2*length-1; //h1、h2分别是两个归并数组的开头,e1、e2是结尾
int k=0,last=0;
while(1){
// printf("%d %d - %d %d\n",h1,e1,h2,e2);
if(e2>=N-1){ //当遍历到最后一组
e2=N-1;
last=1;
}
while(h1<=e1&&h2<=e2){
if(list[h1]<=list[h2]){
sorted[k++]=list[h1++];
}
else{
sorted[k++]=list[h2++];
}
}
while(h1<=e1){
sorted[k++]=list[h1++];
}
while(h2<=e2){
sorted[k++]=list[h2++];
}
h1+=length;
e1+=2*length;
h2+=length;
e2+=2*length;
if(last)
return;
}
}
6-15 Iterative Mergesort (25 分)
最新推荐文章于 2023-07-19 20:11:36 发布