/*
2路归并排序法的基本操作是将待排序列中相邻的两个有序子序列合并成一个有序序列
*/
# include <iostream>
using namespace std;
void Merge(int r1[],int low,int mid,int high,int r[]){
/*
r1[low..mid]和r1[mid+1...high]有序序列,将它们合并成一个有序序列,存放在r[low...high]中
i是r1左子序列的指针
j是r1有子序列的指针
k是r的指针
*/
int i =low;
int j =mid+1;
int k=low;
while((i<=mid)&&(j<=high)){
if(r1[i]<=r1[j]){
r[k] = r1[i];
i++;
}else{
r[k] = r1[j];
j++;
}
k++;
}
//其中一个有序序列比较完毕,则将另一个序列余下的依次存放在r中。
while(i<=mid){
r[k] = r1[i];
k++;
i++;
}
while(j<=high){
r[k] = r1[j];
k++;
j++;
}
}
void printArray(int r[],int length){
for(int i = 0;i<length;i++){
cout<<r[i]<<" ";
}
cout<<endl;
}
void main(){
int r1[10] = {20,25,33,40,11,17,20,35,41,55};
int r[10] = {0,0,0,0,0,0,0,0,0,0};
Merge(r1,0,3,9,r);
printArray(r1,10);
printArray(r,10);
}
2路归并排序法的基本操作是将待排序列中相邻的两个有序子序列合并成一个有序序列
*/
# include <iostream>
using namespace std;
void Merge(int r1[],int low,int mid,int high,int r[]){
/*
r1[low..mid]和r1[mid+1...high]有序序列,将它们合并成一个有序序列,存放在r[low...high]中
i是r1左子序列的指针
j是r1有子序列的指针
k是r的指针
*/
int i =low;
int j =mid+1;
int k=low;
while((i<=mid)&&(j<=high)){
if(r1[i]<=r1[j]){
r[k] = r1[i];
i++;
}else{
r[k] = r1[j];
j++;
}
k++;
}
//其中一个有序序列比较完毕,则将另一个序列余下的依次存放在r中。
while(i<=mid){
r[k] = r1[i];
k++;
i++;
}
while(j<=high){
r[k] = r1[j];
k++;
j++;
}
}
void printArray(int r[],int length){
for(int i = 0;i<length;i++){
cout<<r[i]<<" ";
}
cout<<endl;
}
void main(){
int r1[10] = {20,25,33,40,11,17,20,35,41,55};
int r[10] = {0,0,0,0,0,0,0,0,0,0};
Merge(r1,0,3,9,r);
printArray(r1,10);
printArray(r,10);
}