#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> orignal,target,result;
bool InsertSort(){
int flag = 0;
result = orignal;
for(int i = 1;i<result.size()&&flag<2;i++){
if(flag)
flag++;
int j = i-1;
int ele = result[i];
while(j>=0&&result[j]>ele){
result[j+1] = result[j];
j--;
}
result[j+1] = ele;
if(result == target)
flag = 1;
}
if(flag == 2)
return true;
else return false;
}
void MergeSort(){
result = orignal;
int flag = 0;
vector<int>::iterator it;
for(int i = 2;i<=result.size()&&flag<2;i*=2){
if(flag)
flag++;
int j = 0;
for(it = result.begin();it!=result.end()&&j+i<=result.size();j+=i,it+=i)
sort(it,it+i);
sort(it,result.end());
if(result == target)
flag = 1;
}
}
int main(){
int numofarr;
cin >> numofarr;
int ele;
for(int i = 0;i<numofarr;i++){
cin >> ele;
orignal.push_back(ele);
}
for(int i = 0;i<numofarr;i++){
cin >> ele;
target.push_back(ele);
}
if(InsertSort()){
cout << "Insertion Sort"<<endl;
}
else {
cout << "Merge Sort" << endl;
MergeSort();
}
for(int i = 0;i<result.size();i++){
cout << result[i];
if(i == result.size() - 1)
cout << endl;
else cout << ' ';
}
return 0;
}
PAT(Advance) 1089. Insert or Merge (25)
最新推荐文章于 2020-07-22 22:19:33 发布