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 ;
}

最新文章

  1. View and Data API 现在支持IE11了
  2. mongodb字段类型转化
  3. Eclipse中项目红叉但找不到错误解决方法
  4. 关于 calloc 函数使用 与fun 函数
  5. 如何设计优秀的API(转)
  6. Jquery中的filter()详细说明和transition的用法
  7. javaSE基础之记事本编程
  8. PHP微信支付开发之扫描支付(模式二)后如何回调
  9. (转)Java对象克隆(Clone)及Cloneable接口、Serializable接口的深入探讨
  10. nyoj 正数性质
  11. GIT入门笔记(20)- 使用eclipse 基于 git 开发过程梳理
  12. ACM Smallest Difference
  13. Cocos2D将v1.0的tileMap游戏转换到v3.4中一例(六)
  14. 凸包问题——Graham Scan
  15. JMeter-性能测试监控(解决.sh文件的启动)
  16. WebAPI接口设计:SwaggerUI文档 / 统一响应格式 / 统一异常处理 / 统一权限验证
  17. windows下添加多个git仓库账号
  18. HTML5的新标签之一的Canvas
  19. PAT甲1004 Counting Leaves【dfs】
  20. Spring中的Bean的配置形式

热门文章

  1. uva A Spy in the Metro(洛谷 P2583 地铁间谍)
  2. 优化实例- not in 和 not exists
  3. CF #328div2 D
  4. VBS+bat后强大的功能
  5. 为什么是kafka?
  6. linux openssl 编程 Client端
  7. HibernateBaseDAO
  8. bzoj3297[USACO2011 Open]forgot(dp + string)
  9. BZOJ 3727 DP?推式子..
  10. python2.X现在不能安装Django了:Collecting django Using cached Django-2.0.tar.gz