本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941

1098 Insertion or Heap Sort (25 分)
 

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 (≤). 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.

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.

Sample Input 1:

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

Sample Output 1:

Insertion Sort
1 2 3 5 7 8 9 4 6 0

Sample Input 2:

10
3 1 2 8 7 5 9 4 6 0
6 4 5 1 0 3 2 7 8 9

Sample Output 2:

Heap Sort
5 4 3 1 0 2 6 7 8 9

题目大意: 对N个数据进行排序,给出排序过程中的数据,判断是插入排序还是堆排序,然后输出下一步排序得到的数列。

思路:判断排序方法,前面是从小到大排列,后面的数据跟原始数据相同的就是插入排序,否则为堆排序。

插入排序的下一步操作就是把下一个数字插入到前面合适的位置。

堆排序的首个数字是当前大顶堆的根,将它与堆的最后一个节点(从后往前找,第一个小于根节点的数就是当前堆的最后一个节点)交换位置,然后再对根节点进行堆化操作。

测试点0、2、4是插入排序,1、3、5是堆排序。(不要问我是怎么知道的~)

 #include <iostream>
#include <vector>
using namespace std;
int N;
vector <int> v1, v2;
int maxIndex(int a, int b, int c, int n);
bool fun();
void adjust(int index, int n);
void swap(int& a, int& b);
int main()
{
scanf("%d", &N);
v1.resize(N);
v2.resize(N);
for (int i = ; i < N; i++)
scanf("%d", &v1[i]);
for (int i = ; i < N; i++)
scanf("%d", &v2[i]);
bool flag = fun();//判断排序方式
if (flag) {//插入排序
int index, tmp, j;
for (int i = ; i < N; i++)
if (v2[i] < v2[i - ]) {
index = i;
tmp = v2[index];
break;
}
for (j = index; j > && v2[j - ] > tmp; j--)
v2[j] = v2[j - ];
v2[j] = tmp;
printf("Insertion Sort\n");
}
else {//堆排序
int index, tmp;
for (int i = N - ; i >= ; i--)
if (v2[] > v2[i]) {
index = i;
tmp = v2[index];
break;
}
v2[index] = v2[];
v2[] = tmp;
adjust(, index - );
printf("Heap Sort\n");
}
for (int i = ; i < N; i++) {
printf("%d", v2[i]);
if (i < N - )
printf(" ");
}
return ;
}
void adjust(int index, int n) {
int max;
while (index * + <= n ) {
max = maxIndex(index, index * + , index * + , n);//寻找当前节点与其孩子之间的最大值的下标
if (index == max)//若最大值是自己,则退出循环
break;
swap(v2[index], v2[max]);
index = max;
}
}
int maxIndex(int a, int b, int c, int n) {
if (v2[b] > v2[a])
a = b;
if (c <= n && v2[c] > v2[a])
a = c;
return a;
}
void swap(int& a, int& b) {
int c = a;
a = b;
b = c;
}
bool fun() {
int i, index = ;
for (i = ; i < N; i++)
if (v2[i] < v2[i - ]) {
index = i;
break;
}
for (int i = index; i < N; i++) {
if (v1[i] != v2[i])
return false;
}
return true;
}

PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)的更多相关文章

  1. PAT甲级1098. Insertion or Heap Sort

    PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...

  2. pat 甲级 1098. Insertion or Heap Sort (25)

    1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  3. PAT甲级——A1098 Insertion or Heap Sort

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  4. PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]

    题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...

  5. 1098 Insertion or Heap Sort——PAT甲级真题

    1098 Insertion or Heap Sort According to Wikipedia: Insertion sort iterates, consuming one input ele ...

  6. 1098 Insertion or Heap Sort

    1098 Insertion or Heap Sort (25 分) According to Wikipedia: Insertion sort iterates, consuming one in ...

  7. PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)

    题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...

  8. 【PAT甲级】1098 Insertion or Heap Sort (25 分)

    题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...

  9. PAT (Advanced Level) Practise - 1098. Insertion or Heap Sort (25)

    http://www.patest.cn/contests/pat-a-practise/1098 According to Wikipedia: Insertion sort iterates, c ...

随机推荐

  1. Linux-安装ssh服务

    问题描述: 有些版本的linux系统,如Ubuntn 16 ,安装完成后缺少ssh服务, 所以putty链接会出现访问失败的情况. 解决办法: 在linux中安装ssh服务,并启动 1.安装 sudo ...

  2. 【leetcode刷题笔记】Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  3. 每天一个linux命令(10):touch命令

    版权声明更新:2017-05-14博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的mv命令. 2. ...

  4. ACM学习历程—ZOJ3777 Problem Arrangement(递推 && 状压)

    Description The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem sett ...

  5. ViewModel中C# Property自动添加OnPropertyChanged处理的小工具, 以及相应Python知识点

    在做WPFMVVM中经常会遇到一些Model.ViewModel的属性添加添加私有字段和更改通知方法来支持Binding. 比如把: public class Test {      public s ...

  6. js---window对象的三种窗口

    ============================================================================ window对象的三种窗口.html < ...

  7. python-queue知识点

    1.dict获取value dict.get(key_name)2.三元运算 res,err=stdout.read(),stderr.read() #三元运算 result=res if res e ...

  8. 15、使用ggtree实现进化树的可视化和注释(转载)

    本文作者:余光创,目前就读于香港大学公共卫生系,开发过多个R/Bioconductor包,包括ChIPseeker, clusterProfiler, DOSE,ggtree,GOSemSim和Rea ...

  9. 我的笔记文档版本控制系统-MediaWiki-回到顶部/链接放大/升级

    为了练习自己的JS.CSS基本功,这些天和MediaWiki干上了!^_^ 下面是我的MediaWiki新添加的功能: 回到顶部 链接放大 MediaWiki升级 回到顶部 回到顶部是很多网站的基本功 ...

  10. JSP错误页面

    exception是JSP九大内置对象之一,其实例代表其他页面的异常和错误.只有当页面是错误处理页面时,即isErroePage为 true时,该对象才可以使用.对于C项,errorPage的实质就是 ...