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 ...
随机推荐
- Aladdin and the Flying Carpet LightOJ 1341 唯一分解定理
题意:给出a,b,问有多少种长方形满足面积为a,最短边>=b? 首先简单讲一下唯一分解定理. 唯一分解定理:任何一个自然数N,都可以满足:,pi是质数. 且N的正因子个数为(1+a1)*(1+a ...
- java中的7个位运算运算符
位运算指的是针对整数的二进制进行的位移操作. 位运算提供比算术运算更高的效率,但是位运算的代码可读性较差,建议所有使用位运算的地方写上注释. Java中提供7个位运算符用于位运算. 左移(<&l ...
- sql server锁表、查询被锁表、解锁被锁表的相关语句
MSSQL(SQL Server)在我的印象中很容易锁表,大致原因就是你在一个窗口中执行的DML语句没有提交,然后又打开了一个窗口对相同的表进行CRUD操作,这样就会导致锁表.锁表是一种保持数据一致性 ...
- 源码分析-----ThreadPoolExecutor
创建一个线程池: import time from concurrent.futures import ProcessPoolExecutor, as_completed def get_html(n ...
- 2019-11-29-WPF-绑定命令在-MVVM-的-CanExecute-和-Execute-在按钮点击都没触发可能的原因...
原文:2019-11-29-WPF-绑定命令在-MVVM-的-CanExecute-和-Execute-在按钮点击都没触发可能的原因... title author date CreateTime c ...
- 设置环境变量遇到的难题,cmd管理员方式与普通方式的区别,通过C#代码设置环境变量
在使用mingw64的过程中,需要手工添加环境变量,作为一个懒人,这怎么可以呢?于是想用命令的方式实现,结果遇到问题了,死活实现不了, 之前用过TDM-GCC,人家的安装完就可以用,还有试用过rust ...
- 读取数据,并以txt格式保存
/// <summary> /// 读取数据,并以txt格式保存 /// </summary> /// <param name="data">数 ...
- WPF 时间编辑控件的实现(TimeEditer)
一.前言 有个项目需要用到时间编辑控件,在大量搜索无果后只能自己自定义一个了.MFC中倒是有这个控件,叫CDateTimeCtrl.大概是这个样子: 二.要实现的功能 要实现的功能包含: 编辑时.分. ...
- Go 笔记之如何防止 goroutine 泄露
今天来简单谈谈,Go 如何防止 goroutine 泄露. 概述 Go 的并发模型与其他语言不同,虽说它简化了并发程序的开发难度,但如果不了解使用方法,常常会遇到 goroutine 泄露的问题.虽然 ...
- Java自学-I/O 字符流
Java的字符流 Reader Writer Reader字符输入流 Writer字符输出流 专门用于字符的形式读取和写入数据 步骤 1 : 使用字符流读取文件 FileReader 是Reader子 ...