ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++PTA A1098(插入堆排序)

2022-01-28 16:59:28  阅读:108  来源: 互联网

标签:结点 int true 堆排序 PTA flag tempOri line A1098


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?

插入排序: 迭代,每次重复使用一个输入元素,并生成一个有序的输出列表,每次迭代,插入排序以输入数据中移除一个元素,找到它在已排序列表中的位置,并将其插入,重复操作直到没有元素保留。堆排序划分输入有序序列和没有排序区域,它通过提取最大的元素并将其移动到排序区域,迭代地缩小未排序区域,它使用堆数据结构而不是线性时间搜索来找到最大值。现在给你定一个初始序列,还有有一个序列是某种排序的方式的结果,你能告诉我用了那个排序方式吗?

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.

输入规格:每个输入文件包含一个测试样例,正对每个样例,第一行包含一个正整数N,然后接下来一行N个正整数给你初始序列,最后一行包含,排序序列的结果。假设目标排序总是升序的。所有的数字总是按照空格隔开的。

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.

针对每个测试案例,打印一行要么插入排序,要么堆排序,表示用于获得部分结果的方法。然后在使用一次这样的迭代,第二行输出结果序列,保证答案是针对每个测试文件是唯一的。在一行所有的数字有一个1空格,最后一行没有空格

核心思路

请默写:插入排序sort版本与堆排序(25分)

完整代码

#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 111;
int origin[N], tempOri[N], changed[N];//原始数组,原始数组备份及目标数组
int n; // 元素个数
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; //中间步骤与目标相同,且不是初始序列
        }
        //插入部分直接用sort代替
        sort(tempOri,tempOri+i+1);
        if(flag == true){
            return true;//如果flag为true,则说明已达到目标数组,返回true
        }
    }
    return false;//无法达到目标数组返回false
}
//对heap数组在[low,high]范围进行调整
//其中low为欲调整结点的数组下标high一般为堆的最后一个元素的数组下标
void downAdjust(int low,int high){
    int i = low,j = i*2; //i就是欲调整结点,j为其左孩子结点
    while(j<=high){//存在孩子结点
        //如果右孩子结点存在,且右孩子结点的值大于左孩子结点
        if(j+1 <= high && tempOri[j+1]>tempOri[j]){
            j = j+1;
        }
        //如果孩子结点中最大的权值比父亲结点大
        if(tempOri[j] > tempOri[i]) {
            swap(tempOri[j],tempOri[i]);//交换最大权值的孩子结点与父亲结点
            i = j;
            j = i*2;
        }else{
            break;//孩子结点的权值均比父亲结点的小,调整结束.
        }

    }
}
void heapSort(){
    bool flag = false;
    for(int i =n/2;i>=1;i--){
        downAdjust(i,n);//建堆
    }
    for(int i =n;i>1;i--){
        if(i != n && isSame(tempOri,changed)){
            flag = true;//中间步骤与目标相同,且不是初始序列
        }
        swap(tempOri[i],tempOri[1]);
        downAdjust(1,i-1);//调整堆顶
        if(flag == true){
            showArray(tempOri); // 已达到目标数组,返回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{ //到达此处时一定是堆排序
        printf("Heap Sort\n");
        for(int i =1;i<=n;i++){
            tempOri[i] = origin[i]; //还原tempOri数组
        }
        heapSort(); //堆排序
    }
    return 0;
}


标签:结点,int,true,堆排序,PTA,flag,tempOri,line,A1098
来源: https://blog.csdn.net/m0_37149062/article/details/122732567

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有