C + + PTA a1098 (insert heap sort)

Posted by kayess2004 on Fri, 28 Jan 2022 15:02:11 +0100

According to Wikipedia:

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.

Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

Insert sort: iteration, reuse one input element at a time, and generate an ordered output list. Each iteration, insert sort to remove an element from the input data, find its position in the sorted list, insert it, and repeat the operation until no element is reserved. Heap sorting is divided into input ordered sequence and unordered area. It extracts the largest element and moves it to the sorted area to iteratively reduce the unordered area. It uses heap data structure instead of linear time search to find the maximum value. Now I'll give you an initial sequence, and another sequence is the result of some sort. Can you tell me which sort is used?

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.

Input specification: each input file contains a test sample. For each sample, the first line contains a positive integer N, then the next line contains N positive integers to give you the initial sequence, and the last line contains the results of the sorting sequence. Assume that the target sort is always in ascending order. All numbers are always separated by spaces.

Output Specification:
For each test case, print in the first line either "Insertion Sort" or "Heap Sort" to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resulting sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

For each test case, print a row, either insert sort or heap sort, indicating the method used to obtain partial results. Then, after using one such iteration, the second line outputs the result sequence to ensure that the answer is unique for each test file. All numbers in a line have a 1 space, and there is no space in the last line

Core ideas

Please write: insert sort version and heap sort (25 points)

Complete code

#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 111;
int origin[N], tempOri[N], changed[N];//Original array, original array backup and target array
int n; // Number of elements
bool isSame(int A[], int B[]){
    for(int i =1;i<=n;i++){
        if(A[i] != B[i]) return false;
    }
    return true;
}
bool showArray(int A[]){
    for(int i =1;i<=n;i++){
        printf("%d",A[i]);
        if(i<n) printf(" ");
    }
    printf("\n");
}
bool insertSort(){
    bool flag = false;
    for(int i =2;i<=n;i++){
        if(i!=2 && isSame(tempOri,changed)){
            flag = true; //The intermediate step is the same as the target and is not the initial sequence
        }
        //The insertion part is directly replaced by sort
        sort(tempOri,tempOri+i+1);
        if(flag == true){
            return true;//If the flag is true, it indicates that the target array has been reached and returns true
        }
    }
    return false;//Unable to reach the target. The array returned false
}
//Adjust the heap array in the range of [low,high]
//Where low is the array subscript of the node to be adjusted, and high is generally the array subscript of the last element of the heap
void downAdjust(int low,int high){
    int i = low,j = i*2; //i is the node to be adjusted, and j is its left child node
    while(j<=high){//Child nodes exist
        //If the right child node exists and the value of the right child node is greater than that of the left child node
        if(j+1 <= high && tempOri[j+1]>tempOri[j]){
            j = j+1;
        }
        //If the maximum weight of the child node is greater than that of the father node
        if(tempOri[j] > tempOri[i]) {
            swap(tempOri[j],tempOri[i]);//Exchange the child node and father node with the largest weight
            i = j;
            j = i*2;
        }else{
            break;//The weights of child nodes are smaller than those of parent nodes, and the adjustment is over
        }

    }
}
void heapSort(){
    bool flag = false;
    for(int i =n/2;i>=1;i--){
        downAdjust(i,n);//Build pile
    }
    for(int i =n;i>1;i--){
        if(i != n && isSame(tempOri,changed)){
            flag = true;//The intermediate step is the same as the target and is not the initial sequence
        }
        swap(tempOri[i],tempOri[1]);
        downAdjust(1,i-1);//Adjust the top of the reactor
        if(flag == true){
            showArray(tempOri); // Target array reached, return true
            return ;
        }
    }
}
int main(){
    scanf("%d",&n);
    for(int i =1;i<=n;i++){
        scanf("%d",&origin[i]);
        tempOri[i] = origin[i];
    }
    for(int i = 1;i<=n;i++){
        scanf("%d",&changed[i]);
    }
    if(insertSort()){
        printf("Insertion Sort\n");
        showArray(tempOri);
    }else{ //It must be heap sort when you get here
        printf("Heap Sort\n");
        for(int i =1;i<=n;i++){
            tempOri[i] = origin[i]; //Restore tempOri array
        }
        heapSort(); //Heap sort
    }
    return 0;
}


Topics: C++ Algorithm data structure