PAT(Advance) 1089. Insert or Merge (25)

#include 
#include 
#include 
using namespace std;
vector<int> orignal,target,result;

bool InsertSort(){
    int flag = 0;
    result = orignal;
    for(int i = 1;i2;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;icin >> ele;
        orignal.push_back(ele);
    }
    for(int i = 0;icin >> ele;
        target.push_back(ele);
    }
    if(InsertSort()){
        cout << "Insertion Sort"<else {
        cout << "Merge Sort" << endl;
        MergeSort();
    }
    for(int i = 0;icout << result[i];
        if(i == result.size() - 1)
            cout << endl;
        else cout << ' ';
    }
    return 0;
}

你可能感兴趣的:(Pat)