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)的更多相关文章

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

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

  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 (Advanced Level) 1098. Insertion or Heap Sort (25)

    简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...

  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. 【PAT甲级】1098 Insertion or Heap Sort (25 分)

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

  6. 1098. Insertion or Heap Sort (25)

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

  7. 1098 Insertion or Heap Sort (25分)

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

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

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

  9. pat1098. Insertion or Heap Sort (25)

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

随机推荐

  1. WPF 中如何使得DataGrid的Column有鼠标点击相应

    http://stackoverflow.com/questions/5895803/how-do-i-capture-click-events-on-a-datagrid-column-header ...

  2. Mybatis源码解读-设计模式总结

    虽然我们都知道有26个设计模式,但是大多停留在概念层面,真实开发中很少遇到,Mybatis源码中使用了大量的设计模式,阅读源码并观察设计模式在其中的应用,能够更深入的理解设计模式. Mybatis至少 ...

  3. 多线程Demo1 了解

      首先演示一下主线程的阻塞   //  DYFViewController.m //  623-01-阻塞多线程 // //  Created by dyf on 14-6-23. //  Copy ...

  4. UITableView以及cell属性

    在ios的UI中UITableView是个常用且强大的控件 基本使用: 1>设置代理,一般把控制器设为代理:self.tableView.delegate = self; 2>遵守代理的协 ...

  5. Luogu P1074靶形数独【搜索/剪枝】By cellur925

    题目传送门 显然是一个搜索.但是开始没有任何的剪枝,暴力从\((1,1)\)点开始搜索,很自然地T了6个点. #include<cstdio> #include<algorithm& ...

  6. VC++6.0下新建工程中有17个选项,都是做什么用的?

    要理解每种工程的作用需要很多基础知识,只能简要的和你讲一下: 1.ATL COM AppWizard 用来新建一个COM组件的向导,比如WORD里用的公式编辑器就是一个COM组件. 2.Cluster ...

  7. Cordova 系列之创建一个iOS项目

    1.打开终端 2.输入命令 $ cd Desktop (PS:Desktop表示放在桌面,你可以选择放任意位置) 3.$  cordova create HelloWorld com.example. ...

  8. bzoj2502清理雪道

    传送门 好题啊,由于每个点既可以进,也可以出,就可以新建一个源点和汇点,对于每个点都连边,然后就是最小流板子了. 代码: #include<cstdio> #include<iost ...

  9. 防止a标签跳转的几种方法

    第一种方法 在a标签的href中添加属性值 <a href="javascript:void(0)"></a> 第二种方法 给a标签添加点击事件,函数的返回 ...

  10. (AOP)理解

    AOP的全称: Aspact  Oriented  Programming AOP的目标(作用):让我们可以“专心做事”  日志记录,事务处理,异常捕获,缓存操作. AOP原理 将复杂的需求分解出不同 ...