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 ...
随机推荐
- phpstudy的mysql版本升级至5.7
phpstudy安装的mysql版本一般都是5.5或5.4的,但是有时候做项目又必须用到mysql5.7版本,所以我们现在来看一下如何在phpstudy的环境下将mysql版本升级至5.7 温馨提醒: ...
- jzoj5987. 【WC2019模拟2019.1.4】仙人掌毒题 (树链剖分+概率期望+容斥)
题面 题解 又一道全场切的题目我连题目都没看懂--细节真多-- 先考虑怎么维护仙人掌.在线可以用LCT,或者像我代码里先离线,并按时间求出一棵最小生成树(或者一个森林),然后树链剖分.如果一条边不是生 ...
- 考虑实现Comparable接口
考虑实现Comparable接口 compareTo方法没有在Object中声明.相反,它是Comparable接口中唯一的方法.compareTo方法不但允许进行简单的等同性比较,而且允许执行顺 ...
- sql数据库查询结果字段包含换行符导致复制到Excel发生错位问题的解决
问题描述:在工作过程中,有时会遇到这样的问题,写好sql查询语句在数据库中查询数据,看到行数(比如说是1000行),但是把查询结果复制到Excel里面,却发生了行列错位问题,而导致Excel里面的行数 ...
- Java 执行linux命令(转)
转自 http://blog.csdn.net/a19881029/article/details/8063758 java程序中要执行linux命令主要依赖2个类:Process和Runtime 首 ...
- JQuery | trigger() 方法
trigger() 方法触发被选元素的指定事件类型. 语法格式: trigger(type,[data]) type:触发事件类型 [data]:可选项,表示在触发事件时传递给函数的附加参数. 实例: ...
- java中数据的存放位置
引用自java编程思想四----2.2.1 程序运行时,我们最好对数据保存到什么地方做到心中有数.特别要注意的是内存的分配.有六个地方都可以保存数据:(1) 寄存器.这是最快的保存区域,因为它位于和其 ...
- [題解](單調隊列dp)【2016noip福建夏令營】探險
P1917 -- 探险 时间限制:1000MS 内存限制:131072KB 题目描述(explore.cpp) π+e去遗迹探险,遗迹里有 N 个宝箱,有的装满了珠宝,有的装着废品. π+e ...
- 095 Unique Binary Search Trees II 不同的二叉查找树 II
给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种?例如,给出 n = 3,则有 5 种不同形态的二叉查找树: 1 3 3 2 1 ...
- [已读]移动web手册
冲着作者是PPK才买的,双色,180页,略贵.但是这本书的内容,我并不是很满意.4.5.6三章算是干货,尤其是第四章,值得一读.其他的内容,忽略就好了.