09-排序3 Insertion or Heap Sort
和前一题差不多,把归并排序换成了堆排序。要点还是每一次排序进行判断
开始犯了个错误 堆排序该用origin2 结果一直在排序origin ,误导了半天以为是逻辑错误。。。一直在检查逻辑
建立最大堆
排序并调整下滤
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, NN 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
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std; typedef int ElementType;
bool Judge(int origin[], int changed[], int len)
{
for(int i = ; i < len; i++) {
if(origin[i] != changed[i])
return false;
}
return true;
} //插入排序 (需排序数组,数组长度,排序次数)
void isInsertionSort(ElementType origin[], int N, int times)
{
int i;
ElementType temp = origin[times]; //取出未排序序列中的第一个元素
for (i = times; i > && origin[i-] > temp; i-- )
origin[i] = origin[i-]; //依次与已排序序列中元素比较并右移
origin[i] = temp;
} void Swap( ElementType *a, ElementType *b )
{
ElementType t = *a;
*a = *b;
*b = t;
}
/* 改编PercDown( MaxHeap H, int p )*/
void PercDown( ElementType A[], int p, int N )
{
/* 将N个元素的数组中以A[p]为根的子堆调整为最大堆 */
int Parent, Child; ElementType X = A[p]; /* 取出根结点存放的值 */
for( Parent=p; (Parent*+) < N; Parent=Child ) {
Child = Parent * + ;
if( (Child != N-) && (A[Child] < A[Child+]) )
Child++; /* Child指向左右子结点的较大者 */
if( X >= A[Child] ) break; /* 找到了合适位置 */
else /* 下滤X */
A[Parent] = A[Child];
}
A[Parent] = X;
} void HeapSort(ElementType A[], int N, int changed[])
{
for(int i = N/-; i >= ; i--) /* 建立最大堆 */
PercDown( A, i, N ); for(int i = N-; i > ; i--) {
/* 删除最大堆顶 */
Swap(&A[], &A[i] );
PercDown(A, , i); if( Judge(A, changed, N) ) { //是堆排序
printf("Heap Sort\n"); Swap(&A[], &A[i-] ); //在执行一次堆排序
PercDown(A, , i-);
for(int j = ; j < N-; j++)
printf("%d ",A[j]);
printf("%d\n",A[N-]);
return;
}
}
} int main()
{
int N;
int origin[],origin2[],changed[];
scanf("%d", &N);
for(int i = ; i < N; i++) { //origin origin1 初始序列
scanf("%d",&origin[i]);
origin2[i] = origin[i];
}
for(int i = ; i < N; i++) //changed 排序后序列
scanf("%d",&changed[i]); for(int i = ; i < N; i++) {
isInsertionSort(origin, N, i);
if( Judge(origin, changed,N) ) { //是插入排序
printf("Insertion Sort\n");
isInsertionSort(origin, N, i+);
for(int j = ; j < N-; j++)
printf("%d ",origin[j]);
printf("%d\n",origin[N-]);
return ;
}
} HeapSort(origin2,N,changed);
return ;
}
09-排序3 Insertion or Heap Sort的更多相关文章
- PAT甲级1098. Insertion or Heap Sort
PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...
- 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 ...
- pat 甲级 1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT_A1098#Insertion or Heap Sort
Source: PAT_A1098 Insertion or Heap Sort (25 分) Description: According to Wikipedia: Insertion sort ...
- 1098 Insertion or Heap Sort——PAT甲级真题
1098 Insertion or Heap Sort According to Wikipedia: Insertion sort iterates, consuming one input ele ...
- PTA Insertion or Heap Sort
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 1098 Insertion or Heap Sort
1098 Insertion or Heap Sort (25 分) According to Wikipedia: Insertion sort iterates, consuming one in ...
- 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 ...
- Insertion or Heap Sort
7-14 Insertion or Heap Sort(25 分) According to Wikipedia: Insertion sort iterates, consuming one inp ...
随机推荐
- js数据结构与算法存储结构
数据结构(程序设计=数据结构+算法) 数据结构就是关系,没错,就是数据元素相互之间存在的一种或多种特定关系的集合. 传统上,我们把数据结构分为逻辑结构和物理结构. 逻辑结构:是指数据对象中数据元素之间 ...
- 对iframe跨域通信的封装
github源码:https://github.com/boycy815/topProxy 为了偷懒所以依赖了Kissy:http://docs.kissyui.com/ 用法举例:需求是在http: ...
- Custom Date tag
Custom Date tag: custom date based on pattern format. Default date is current day. <CUSTOMDATE[+, ...
- spring batch学习笔记
Spring Batch是什么? Spring Batch是一个基于Spring的企业级批处理框架,按照我师父的说法,所有基于Spring的框架都是使用了spring的IoC特性,然后加上 ...
- 《App研发录》知识点汇总
原文链接:http://www.jianshu.com/p/fc8c4638937e <App研发录>这部书是包建强写的,说来也巧,在读这边书之前在看池建强的<Mac 人生元编程&g ...
- 二模08day2解题报告
T1.引爆炸弹(bomb) N个炸弹构成一棵树,引爆一颗叶节点,会一直引爆到根节点.每颗炸弹有一个价值,求引爆k个炸弹的最大价值. 既然是一棵树,那么自然想到dp.所以先树形dp了一遍(由于可能出现多 ...
- Android——Runtime类中的freeMemory,totalMemory,maxMemory等几个方法
maxMemory() 这个方法返回的是java虚拟机(这个进程)能构从操作系统那里挖到的最大的内存,以字节为单位,如果在运行java程序的时 候,没有添加-Xmx参数,那么就是64兆,也就是说max ...
- java异常处理的两种方法
一种是try-catch-finally,监视代码段,如果有异常就捕获. 另一种是此处不处理,声明在方法后面,抛给上级.(不处理也是一种处理)
- CSS阻止页面双击选中文本
转载自:w3cui 在双击左右箭头,快速切换图片滚动时,会选择附近区域的文字,感觉不是很好,今天在同事在分享时,讲到了这个问题, 试了一下,不错,解决了问题IE及Chrome下的方法一样,对相应的元素 ...
- 用verilog模拟DDS产生正弦波信号
前言: DDS:直接数字频率合成,正弦波0-2pi周期内,相位到幅度是一一对应的(这里我们使用放大后的整数幅度). 主要思路: 个人理解,FPGA不擅长直接做数字信号计算,那样太占用片上逻辑资源,所以 ...