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 ...
随机推荐
- 解决Docker服务无法正常启动
重新docker服务报错如下: systemctl restart docker.service Cannot connect to the Docker datemon at tcp://0.0.0 ...
- [转帖]Linux date命令的用法(转)
Linux date命令的用法(转) https://www.cnblogs.com/asxe/p/9317811.html 1.命令:date 2.命令功能:date 可以用来显示或设定系统的日期与 ...
- 详解XOR(异或)运算加密
逻辑运算之中,除了 AND 和 OR,还有一种 XOR 运算,中文称为"异或运算".它的定义是:两个值相同时,返回false,否则返回true.也就是说,XOR可以用来判断两个值是 ...
- scala中val和var的区别
1:内容是否可变:val修饰的是不可变的,var修饰是可变的 2:val修饰的变量在编译后类似于java中的中的变量被final修饰 3:lazy修饰符可以修饰变量,但是这个变量必须是val修饰的 p ...
- Jenkins生成APK链接的二维码
Window环境 1.下载安装Python如3.7.5版本,安装Python到电脑上如C:\Python37,将C:\Python37,C:\Python37\Scripts添加到Path环境变量中 ...
- mybatis分页的一种解决方案
mybatis自定义分页解决方案 1.PageSqlProvider<T> —— 提供默认的分页列表查询 package com.xinyartech.erp.core.base; im ...
- Vector线程安全,ArrayList非线程安全
http://baijiahao.baidu.com/s?id=1638844080997170869&wfr=spider&for=pc Vector线程安全,ArrayList非线 ...
- C#使用post方式提交json数据
尝试了一天,尝试了各种方法,一下方法最直接方便. //地址 string _url = "https://www.dXXXayup.ink/api/User/Login"; //j ...
- 检测代码潜在bug和质量之SonarQube
参数使用 项目分析参数可以在多个地方设置,继承关系如下: 全局分析参数,通过Web UI设置,作用于所有项目(配置–>通用–>通用中设置) 项目分析参数,通过WebUI设置,覆盖全局参数( ...
- python动态视频下载器
这里向大家分享一下python爬虫的一些应用,主要是用爬虫配合简单的GUI界面实现视频,音乐和小说的下载器.今天就先介绍如何实现一个动态视频下载器. 爬取电影天堂视频 首先介绍的是python爬取电影 ...