Source:

PAT_A1098 Insertion or Heap Sort (25 分)

Description:

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.

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

keys:

Attention:

  • 初始状态与中间状态相同时为插入排序;
  • 初始状态与中间状态相同时,下一个前插的元素应该是A【k】< A【k-1】的元素,而非直接把第二个元素前插(初始状态 != 中间状态);

Code:

 /*
Data 2019-06-21 17:11:02
Problem: PAT_A1098#Insertion or Heap Sort
AC: 41:05 题目大意:
给定初始序列和经过若干阶段排序的中间序列,判断是插入排序还是堆排序
设最终序列递增有序
输出:
插入排序或堆排序,并给出下一阶段的排序结果 基本思路:
根据插入排序的特点来判断,
前半部分递增,后半部分与初始序列相同,则为插入排序
注意:初始序列与中间序列相同,为插入排序
*/
#include<cstdio>
#include<algorithm>
using namespace std;
const int M=;
int n, init[M], sorted[M]; void DownAdjust(int low, int high)
{
int i=low, j=i*;
while(j<=high)
{
if(j+<=high && sorted[j+]>sorted[j])
j++;
if(sorted[i] < sorted[j])
{
swap(sorted[i], sorted[j]);
i=j;
j=i*;
}
else
break;
}
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%d", &n);
for(int i=; i<=n; i++)
scanf("%d", &init[i]);
for(int i=; i<=n; i++)
scanf("%d", &sorted[i]);
int pos=n;
while(pos>= && init[pos]==sorted[pos])
pos--;
int pt=pos;
while(pos>= && sorted[pos-]<=sorted[pos])
pos--;
if(pos==)
{
printf("Insertion Sort\n");
if(pt==)
while(pt<=n && sorted[pt]<=sorted[pt+])
pt++;
sort(sorted+,sorted+pt+);
}
else
{
printf("Heap Sort\n");
pt=n;
while(pt>= && sorted[]<=sorted[pt])
pt--;
swap(sorted[], sorted[pt]);
DownAdjust(,pt-);
}
for(int i=; i<=n; i++)
printf("%d%c", sorted[i], i==n?'\n':' '); return ;
}

PAT_A1098#Insertion or Heap Sort的更多相关文章

  1. PTA Insertion or Heap Sort

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

  2. 1098 Insertion or Heap Sort

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

  3. PAT甲级1098. Insertion or Heap Sort

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

  4. PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...

  5. pat1098. Insertion or Heap Sort (25)

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

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

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

  7. PTA 09-排序3 Insertion or Heap Sort (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/676 5-14 Insertion or Heap Sort   (25分) Accor ...

  8. Insertion or Heap Sort

    7-14 Insertion or Heap Sort(25 分) According to Wikipedia: Insertion sort iterates, consuming one inp ...

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

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

随机推荐

  1. LinkedList,ArrayList末尾插入谁效率高?

    废话不多说,原因不解释.上測试代码: package com.letv.cloud.cdn.jtest; import java.io.IOException; import java.util.Ar ...

  2. effective C++ 读书笔记 条款11

    条款11: 在operator= 中处理"自我赋值" 在实现operator=时考虑自我赋值是必要的就像 x=y .我们不知道变量x与y代表的值是否为同一个值(把x和y说成是一个指 ...

  3. 【HDOJ 2255】奔小康赚大钱(KM算法)

    [HDOJ 2255]奔小康赚大钱(KM算法) 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  4. wox 快速搜索程序

    windows启动栏的搜索,会经常找不到exe. 使用wox可以非常快速的找到启动程序 https://github.com/Wox-launcher/Wox/ 安装完成后,默认alt+space出现 ...

  5. Swift版本UIWebView长按保存图片

    起因 最近需要做个IOS的壳子,用到长按保存图片的功能,发现百度出来的全是OC语法的例子,很多都不是全面,只能自己写一份Swift版本的,图片下面附上Github地址 效果图 Github地址:htt ...

  6. hdu 2988(最小生成树 kruskal算法)

    Dark roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. B1922 [Sdoi2010]大陆争霸 最短路

    我一直都不会dij的堆优化,今天搞了一下...就是先弄一个优先队列,存每个点的数据,然后这个题就加了一点不一样的东西,每次的最短路算两次,一次是自己的最短路,另一次是机关的最短路,两者取最大值才是该点 ...

  8. HTTP权威协议笔记-10.HTTP-NG

    1.HTTP发展中存在的问题 复杂性   其连接.报文.及功能逻辑之间的混合使用相当复杂,使用容易出错 可扩展性 传统流行下来的http应用很难实现扩展性,且无法兼容 性能      高延时.低吞吐 ...

  9. 练习2 及pl/sql

    Rownum 如果不是对主键排序是不会变得 -查询没有学分的学生信息 --SELECT * FROM z_student zs WHERE zs.code NOT IN (SELECT DISTINC ...

  10. BZOJ 2118 Dijkstra

    思路: 经典题 不解释 找到最小的数mn 所有都是在mod mn的意义下 搞得 i->(i+a[i])%mn  边权为a[i] //By SiriusRen #include <queue ...