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, 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 resuling 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 此题是2015年春季的研究生入学考试复试时的机试题,链接 http://www.patest.cn/contests/graduate-entrance-exam-2015-03-20
#include<cstdio>
#include<cstring> void headshift(int *a,int i,int len)//数组 要处理的节点下标 当前处理的堆的长度
{
if(i>=len/ || i<) return;//叶节点可看作一个满足性质的堆,所以只对非叶子结点进行处理即可
int max=i; //调整一个节点为(节点值、左孩子节点值、右孩子节点值)三者中的最大值
if(*i+<=len- && a[max]<a[*i+]) max=*i+;
if(*i+<=len- && a[max]<a[*i+]) max=*i+;
if(max!=i)
{
int temp=a[max];
a[max]=a[i],a[i]=temp;
headshift(a,max,len);//某节点与其某个孩子节点的数值交换调整后,可能会影响该孩子节点的堆的性质,所以要向下调整
}
return;
} int match(int *temp,int *a,int len)
{
for(int i=;i<len;i++)
if(temp[i]!=a[i]) return ;
return ;
} void print(int *a,int len)
{
for(int i=;i<len;i++)
{
if(i) printf(" %d",a[i]);
else printf("%d",a[i]);
}
return;
} void Insertion(int *a,int i)
{
int loc=i-,temp=a[i];
for(;loc>=;loc--) if(a[loc]<=temp) break;//寻找应该插入的位置
loc++;
if(loc!=i) for(int j=i;j>loc;j--) a[j]=a[j-];//比当前处理值大的元素相应后移
a[loc]=temp;
}
int main()
{ int num=,data[]={},sorttemp[]={},a[]={};
scanf("%d",&num);
for(int i=;i<num;i++) scanf("%d",data+i);
for(int i=;i<num;i++) scanf("%d",sorttemp+i); for(int i=;i<num;i++) a[i]=data[i];
for(int i=;i<num;i++) //i=0时,不构成插入排序的定义 因为0元素之前没有有序子序列
{
Insertion(a,i);
if(match(sorttemp,a,num))//if(match) 再处理一轮+输出+return
{
if(i<num-) i++; //i++易漏掉 one more iteration
Insertion(a,i);
printf("Insertion Sort\n");
print(a,num);
return ;
}
} for(int i=;i<num;i++) a[i]=data[i];
int len=num,temp=; for(int i=num/-;i>=;i--) //构建大根堆
headshift(a,i,len); for(int i=num-;i>=;i--) //排序
{
temp=a[],a[]=a[i],a[i]=temp;//顶尾交换 表示当前最大值已处理
len--;//堆长度-1
headshift(a,,len);//headshift新顶
if(match(sorttemp,a,num))//if(match) 再处理一轮+输出+return
{
i--; //i-- 易漏掉 one more iteration
temp=a[],a[]=a[i],a[i]=temp;
len--;
headshift(a,,len);
printf("Heap Sort\n");
print(a,num);
return ;
}
}
return ;
}
PAT (Advanced Level) Practise - 1098. Insertion or Heap Sort (25)的更多相关文章
- PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...
- pat 甲级 1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT (Advanced Level) 1098. Insertion or Heap Sort (25)
简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...
- PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]
题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...
- 【PAT甲级】1098 Insertion or Heap Sort (25 分)
题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...
- 1098. Insertion or Heap Sort (25)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 1098 Insertion or Heap Sort (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...
- pat1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
随机推荐
- MFC——ComBox用法大全
Combo Box (组合框)控件很简单,可以节省空间.从用户角度来看,这个控件是由一个文本输入控件和一个下拉菜单组成的.用户可以从一个预先定义的列表里选择一个选项,同时也可以直接在文本框里面输入文本 ...
- Unity3d的批渲染 batch rendering
http://blog.csdn.net/leonwei/article/details/41942157 批渲染(Batch) batch render 是大部分引擎提高渲染效率的方法,基本原理就是 ...
- MonogoDb的角色分类
引用: http://blog.csdn.net/kk185800961/article/details/45619863 https://docs.mongodb.org/manual/refer ...
- IT兄弟连 JavaWeb教程 JSP内置对象2
application对象 application对象用于保存所有应用程序中的公有数据.它在服务器启动时自动创建,在服务器关闭时销毁,当application对象没有被销毁时,所有用户都可以共享app ...
- 怎么解决UIScrollView把uitableviewcell的点击事件屏蔽了
[self.contentView addSubview:self.scrollView]; self.scrollView.userInteractionEnabled = NO; [self.co ...
- Dwarves, Hats and Extrasensory Abilities Codeforces - 1063C
https://codeforces.com/contest/1063/problem/C 首先可以想到一个简单做法:先钦定这个直线的斜率k=-1,然后设直线y=-x+b 设黑点放直线上方:如果已知( ...
- 子shell
http://bbs.csdn.net/topics/392292455 https://www.cnblogs.com/daniaoge/p/6161821.html http://blog.csd ...
- vue.js 中如何解除绑定事件
我们项目中有一个点赞需求,只允许点击一次赞,再次点击则取消赞, 为了防止用户多次连续点击,在点赞后需要解绑事件,成功调取API后,才可再次点击取消赞. 目前用的方法是加入一个flag控制点击事件可否点 ...
- python学习之图形界面编程:
一 tkinter:tkinter是python自带的支持tk的库,python代码调用tkinter->tk->操作系统提供的本地GUI(TKL语言开发))完成界面开发,不需要安装任何第 ...
- 18002 Z-Scan 模拟题
18002 Z-Scan 时间限制:1000MS 内存限制:65535K提交次数:0 通过次数:0 题型: 编程题 语言: 不限定 Description Z-Scan is a method ...