PAT(Advance) 1098. Insertion or Heap Sort (25)

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

bool InsertSort(){
    int flag = 0;
    //result.clear();
    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 PrecDown(int i,int end){
    int tmp,child;
    for(tmp = result[i];2*i + 12*i +1;
        if(child!=end-1&&result[child]1])
            child++;
        if(tmpelse break;
    }
    result[i] = tmp;
}

void HeapSort(){
    result = orignal;
    for(int i = result.size()/2;i>=0;i--)
        PrecDown(i,result.size());
    int flag = 0;
    for(int i = result.size()-1;i>=0&&flag<1;i--){
        //if(flag)
          //  flag++;
        if(result == target)
            flag = 1;
        int tmp = result[0];
        result[0] = result[i];
        result[i] = tmp;
        PrecDown(0,i);
    }
}

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 << "Heap Sort" << endl;
        HeapSort();
    }
    for(int i = 0;icout << result[i];
        if(i == result.size() - 1)
            cout << endl;
        else cout << ' ';
    }
    return 0;
}

你可能感兴趣的:(Pat)