【样例输入】
5
11 8 34 2 9【样例输出】
2 8 9 11 34
#include<iostream>
using namespace std;
int n;//n个数
int data[100];
int tmp[100];
int nums = 1;
void ss(){
cout<<endl<<"-----------------"<<endl;
for(int i=1;i<=n;i++){
cout<<data[i]<<" ";
}
cout<<endl<<"-----------------"<<endl;
}
void hebing(int start,int mid,int end ){
int POS1 = start;
int POS2 = mid+1;
int old = nums;
while(POS1<=mid&&POS2<=end){
if(data[POS1]>=data[POS2]){
tmp[nums++] = data[POS2++];
}else{
tmp[nums++] = data[POS1++];
}
}
while(POS2<=end){
tmp[nums++] = data[POS2++];
}
while(POS1<=mid){
tmp[nums++] = data[POS1++];
}
cout<<endl<<"@@@@@@@@@@@@@@@@"<<endl;
for(int i=start;i<=end;i++){
cout<<tmp[i]<<" ";
}
cout<<endl;
copy(tmp+old,tmp+nums,data+start);
ss();
return ;
}
void huafen(int start,int end){
if(start<end){
int mid = (start+end)/2;// 中间值的下标
cout<<"划分的中间值:"<<start<<"##"<<end<<":"<<mid<<endl;
huafen(start,mid);
huafen(mid+1,end);
hebing(start,mid,end); // 合并操作
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++){
cin >> data[i];
}
huafen(1,n);
return 0;
}
//8
//60 71 49 11 82 49 3 66