1098 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 (≤). 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
- #define _CRT_SECURE_NO_WARNINGS
- #include<stdio.h>
- #include<malloc.h>
- #include<stdlib.h>
- int A[];
- int B[];
- int L;
- void Swap(int *C,int i, int j)
- {
- int temp = C[i];
- C[i] = C[j];
- C[j] = temp;
- }
- void Print(int N)
- {
- int i;
- for (i = ; i < N - ; i++)
- printf("%d ", B[i]);
- printf("%d", B[i]);
- }
- int Charge(int N)
- {
- int i;
- if (B[] > B[] && B[] > B[])
- return ;
- else
- return ;
- }
- void Insert_Once(int i, int N)
- {
- int j;
- int Temp = B[i];
- for (j = i; j > && B[j - ] > Temp; j--)
- B[j] = B[j - ];
- B[j] = Temp;
- }
- void PrecDown(int i,int N)
- {
- int Parent, Child;
- int Tmp = A[i];
- for (Parent = i; Parent * + <= N; Parent = Child)
- {
- Child = Parent * + ;
- if (Child != N && A[Child] < A[Child + ])
- Child++;
- if (Tmp >= A[Child])break;
- else
- A[Parent] = A[Child];
- }
- A[Parent] = Tmp;
- }
- void BuildMaxHeap(int N)
- {
- for (int i = (N - ) / ; i >= ; i--)
- PrecDown(i, N - );
- }
- void Heap_Sort(int N)
- {
- for (int i = N - ; i >= ; i--)
- {
- Swap(A, , i);
- PrecDown(, i-);
- }
- }
- int FindI(int N)
- {
- int i;
- for (i = N - ; i >= && A[i] == B[i]; i--);
- return i;
- }
- void PrecDown_Once(int i)
- {
- Swap(B,, i);
- int Parent, Child;
- int tmp;
- tmp = B[];
- for (Parent = ; (Parent * ) + <= i-; Parent = Child)
- {
- Child = (Parent * ) + ;
- if (Child != i-&& B[Child]<B[Child + ])
- Child++;
- if (tmp >= B[Child])break;
- else
- B[Parent] = B[Child];
- }
- B[Parent] = tmp;
- }
- void Heap_Once(int N)
- {
- BuildMaxHeap(N);
- Heap_Sort(N);
- int i = FindI(N);
- PrecDown_Once(i);
- }
- int main()
- {
- int N;
- int Flag = ;
- int OrderPosition = ;
- scanf("%d", &N);
- for (int i = ; i < N; i++)
- scanf("%d", &A[i]);
- for (int i = ; i < N; i++)
- scanf("%d", &B[i]);
- if (Flag = Charge(N))
- printf("Heap Sort\n");
- else
- printf("Insertion Sort\n");
- for (int i = ; i < N - ; i++)
- if (B[i] <= B[i + ]) //这里必须是大于等于
- OrderPosition = i + ;
- else
- break;
- if (Flag)
- Heap_Once(N);
- else
- Insert_Once(OrderPosition + , N);
- Print(N);
- return ;
- }
1098 Insertion or Heap Sort (25分)的更多相关文章
- 【PAT甲级】1098 Insertion or Heap Sort (25 分)
题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...
- pat 甲级 1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 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 (Advanced Level) Practise - 1098. Insertion or Heap Sort (25)
http://www.patest.cn/contests/pat-a-practise/1098 According to Wikipedia: Insertion sort iterates, c ...
- 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 ...
- PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...
- 09-排序3 Insertion or Heap Sort (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]
题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...
随机推荐
- python 异步请求
这是循环请求10次页面.总时间大概是10秒左右,如果是普通的循环请求10次页面而不添加异步的话,时间大概在30秒以上,当然这个数据可能有误,因为有网速的问题存在,但大体的效果应该是不变的. impor ...
- WINDOWS上JDK安装与环境变量设置
一.JDK安装 jdk版本:jdk1.8.0_144 下载链接:https://pan.baidu.com/s/1eS2bFhg 密码:e3q1 下载JDK后点击安装,可以根据需要修改JDK的安装目录 ...
- docker的安装,自己写了一个安装docker的脚本,辅助做docker安装的实验(ubuntu)
#!/bin/bash #获取用户名 [ pwd == '/root' ] && hn="root@$(hostname):~#" || hn="root ...
- 深入理解JS引擎的执行机制
深入理解JS引擎的执行机制 1.灵魂三问 : JS为什么是单线程的? 为什么需要异步? 单线程又是如何实现异步的呢? 2.JS中的event loop(1) 3.JS中的event loop(2) 4 ...
- Java 并发容器(转)
转自:https://blog.ouyangsihai.cn/%2Fjava-gao-bing-fa-zhi-bing-fa-rong-qi-xiang-jie-cong-ru-men-dao-cha ...
- .NET 5 Preview 1的深度解读和跟进
这几天微软.NET 团队发布了.NET 5 Preview-1, 如约而至.很兴奋,因为.NET Core和.NET Framework终于实现了大一统,同时也很期待,期待.NET 5能给我们带来哪些 ...
- [剑指offer]14-1.剪绳子
14-1.剪绳子 方法一 动态规划 思路:递归式为f(n)=max(f(i), f(n-i)),i=1,2,...,n-1 虽然我现在也没有彻底明白这个递归式是怎么来的,但用的时候还是要注意一下.f( ...
- P4147 玉蟾宫 题解
原题链接 简要题意: 求最大 \(0\) 矩阵.(将字符转化为数字) 本题是模板题,可以用来爆踩.??? 悬线法 来了! 其中绿色是 \(0\),红色是 \(1\). 下面以这个图为例讲一下算法流程. ...
- 洛谷 P5658 括号树 题解
原题链接 简要题意: 求出以从每个节点到根形成的括号序列的合法对数. 算法一 观察到 \(n \leq 8\) ,所以我们可以用 纯粹的暴力 . 用 \(O(n)\) 时间得出当前节点到根的字符串. ...
- 常见排序算法总结与分析之交换排序与插入排序-C#实现
前言 每每遇到关于排序算法的问题总是不能很好的解决,对一些概念,思想以及具体实现的认识也是模棱两可.归根结底,还是掌握不够熟练.以前只是看别人写,看了就忘.现在打算自己写,写些自己的东西,做个总结.本 ...