09-排序3 Insertion or Heap Sort (25 分)
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
#include<cstdio>
#include<algorithm>
using namespace std; const int maxn = ;
int tempOri[maxn], origin[maxn], change[maxn];
int n; bool Insertion();
bool isSame(int A[], int B[]);
void showArray(int A[]);
void Heap();
void downjust(int low, int high); int main()
{ scanf("%d",&n);
for (int i = ; i <= n; i++)
{
scanf("%d",&origin[i]);
tempOri[i] = origin[i];
} for (int i = ; i <= n; i++)
{
scanf("%d",&change[i]);
} if (Insertion())
{
printf("Insertion Sort\n");
showArray(tempOri);
}
else
{
printf("Heap Sort\n");
for (int i = ; i <= n; i++)
{
tempOri[i] = origin[i];
}
Heap();
} return ;
} bool Insertion()
{
bool flag = false;
for (int i = ; i <= n; i++)
{
if (i != && isSame(tempOri, change))
{
flag = true;
} sort(tempOri,tempOri+i+);
if (flag)
{
return true;
}
}
return false;
} bool isSame(int A[], int B[])
{
for (int i = ; i <= n; i++)
{
if (A[i] != B[i])
{
return false;
}
}
return true;
} void showArray(int A[])
{
for (int i = ; i <= n; i++)
{
printf("%d",A[i]);
if (i < n)
{
printf(" ");
}
}
} void Heap()
{
bool flag = false;
for (int i = n/; i >= ; i--)
{
downjust(i,n);
} for (int i = n; i > ; i--)
{
if (i != n && isSame(tempOri, change))
{
flag = true;
}
swap(tempOri[i], tempOri[]);
downjust(, i-); if (flag)
{
showArray(tempOri);
return;
}
}
} void downjust(int low, int high)
{
int parent = low, child = parent * ;
while (child <= high)
{
if (child < high && tempOri[child+] > tempOri[child])
{
child++;
} if (tempOri[child] > tempOri[parent])
{
swap(tempOri[child], tempOri[parent]);
parent = child;
child *= ;
}
else
{
break;
}
}
}
09-排序3 Insertion or Heap Sort (25 分)的更多相关文章
- 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 ...
- 【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 ...
- 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甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...
- PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]
题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...
- 1098. Insertion or Heap Sort (25)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PAT (Advanced Level) 1098. Insertion or Heap Sort (25)
简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...
随机推荐
- javascript 对象,函数,原型和 this
1.对象 在javascript里,一切都是对象,包括函数自身(不是指具体的函数,而是指"Function"这个东东).例如: var fun1=new Function(&quo ...
- [完整版]Postgresql 数据库 备份以及恢复的过程
0. 准备工作 linux机器上面 必须安装上pg数据库 然后 需要将 pg的主程序目录 放到环境变量里面去 便于执行命令. 1. 先备份 . 备份目标数据库: pg_dump -h -F c -f ...
- 用siege测试接口高并发
siege -c 255 -r 2555 "http://10.1.1.6:3001/decode POST <./api.json" -t 100s
- 关于 Task.Run 简单的示例
1. 关于 Task.Run 简单的示例01 直接贴代码了: public static class TaskDemo01 { public static void Run() { Console.W ...
- 信安周报-第04周:系统函数与UDF
信安之路 第04周 前言 这周自主研究的任务如下: 附录解释: SQLi的时候应对各种限制,可以使用数据库自带的系统函数来进行一系列变换绕过验证 eg:字符串转换函数.截取字符串长度函数等 注入的时候 ...
- sql比较字符串,比的到底是什么?
sql里有nvarchar类型的日期数据time1:2019-10-09 00:00:00, 现给定string类型日期time2:2019-10-01 23:59:59,比较两个日期的大小, 发现可 ...
- .NET EF执行sql报数组超出了索引
使用ef查询,写sql语句的 一般情况报数组超出了索引都认为是[i]里面的值超出了,但是执行sql报超出了索引,让人很蒙 在网上找了半天也没有结果,后来只能自己来解决了. 在异常里面能看到dbnull ...
- Delphi - 10进制16进制相互转换
10进制转16进制 使用IntToHex可以实现十进制到十六进制的转换,注意这里的参数有两个,第一个表示需要被转换的10进制数,第二个表示转换后用几位来显示16进制数. 代码如下: function ...
- css+js实现自动伸缩导航栏
用css+js实现自动伸缩导航栏 需要达到的效果: 默认首页选中样式 设置鼠标滑过效果:颜色变化(#f60),宽度变化,字体变化 所涉及的知识点: 布局:float css: 元素状态切换(displ ...
- loj#10172 涂抹果酱 (状压DP)
题目: #10172. 「一本通 5.4 练习 1」涂抹果酱 解析: 三进制的状压DP 经过简单的打表发现,在\(m=5\)时最多有\(48\)种合法状态 然后就向二进制一样枚举当前状态和上一层的状态 ...