PAT甲级 - 1098 Insertion or Heap Sort (25 分) 堆排序,找规律

PAT甲级真题 1098 Insertion or Heap Sort (25分) C++实现(插入排序、堆排序) 题目 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 阅读详情

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

纸上得来终觉浅,觉知此事要躬行。

  1. 涉及到HeapSort,数组从1开始计数
  2. 初始序列不能参与比较
  3. HeapSort(),从n/2开始,循环初始化。 
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 110;
vector<int>origin, target, temp; //temp进行一步步排序,然后与changed进行比较
int n;

bool InsertSort() {	int flag = 0;
	for (int i = 2; i <= n; i++) {
		sort(temp.begin() + 1, temp.begin() + 1 + i);
		if (temp == target)
		{
			sort(temp.begin() + 1, temp.begin() + i + 2);
			flag = 1;
			break;
		}
	}
	if (flag == 1) {
		printf("Insertion Sort\n");
		for (auto it = temp.begin() + 1; it != temp.end(); it++) {
			if (it != temp.begin() + 1)
				printf(" ");
			printf("%d", *it);
		}
	}
	return flag;
}
void downAdjust(vector<int>& temp, int low, int high) { //插入堆后,downAdjust
	int i = low, j = i * 2;
	while (j <= high) {
		if (j + 1 <= high && temp[j + 1] > temp[j]) {
			j = j + 1;
		}
		if (temp[j] > temp[i]) { //如果根节点不是最大的
			swap(temp[i], temp[j]);
			i = j;
			j = j * 2;
		}
		else {
			break;
		}
	}
}
void HeapSort() {
	int flag = 0;
	for (int i = n / 2; i >= 1; i--) {
		downAdjust(temp, i, n);
	}
	for (int i = n; i >= 1; i--)
	{
		swap(temp[i], temp[1]);
		downAdjust(temp, 1, i - 1);
		if (flag == 1)
			break;
		if (temp == target) {
			flag = 1;
		}
	}
	if (flag == 1) {
		for (auto it = temp.begin() + 1; it != temp.end(); it++) {
			if (it != temp.begin() + 1)
				printf(" ");
			printf("%d", *it);
		}
	}
}
int main() {
	scanf("%d", &n);
	int v;
	origin.push_back(-1);
	temp.push_back(-1);
	target.push_back(-1);
	for (int i = 0; i < n; i++) {
		scanf("%d", &v);
		origin.push_back(v);
		temp.push_back(v);
	}
	for (int i = 0; i < n; i++) {
		scanf("%d", &v);
		target.push_back(v);
	}
	if (!InsertSort()) {
		printf("Heap Sort\n");
			for (int i = 1; i < origin.size(); i++) {
				temp[i] = origin[i];
			}
		HeapSort();
	}

	return 0;
}

但是,如果仅仅是为了完成这道题的话,可以有更简单的思路来做:

/*
insert sort的特点,前p个数字有序,后n-p个数字顺序不变
heap sort特点,后p个数字有序,且都大于第一个数字,第一个数字是n-p序列的最大值
*/
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

void downAdjust(vector<int>& b, int low, int high) {
	int i = low, j = i * 2;
	while (j <= high) {
		if (j + 1 <= high && b[j + 1] > b[j]) j++;
		if (b[i] < b[j])
		{
			swap(b[i], b[j]);
			i = j;
			j = i * 2;
		}
		else break;
	}
}
int main() {
	int n, p = 2;//p是游标
	scanf("%d", &n);
	vector<int> a(n + 1), b(n + 1); // 涉及到堆,序列从1开始
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
	while (p <= n && b[p - 1] <= b[p]) p++;
	int index = p;
	while (p <= n && a[p] == b[p]) p++;
	if (p == n + 1) {
		printf("Insertion Sort\n");
		sort(b.begin() + 1, b.begin() + 1 + index); //排序,排到应排序列的下一个元素
	}
	else { //那么一定是堆排序,从后向前找到第一个小于b[1]的数即可
		printf("Heap Sort\n");
		p = n;//从后向前遍历
		while (p > 2 && b[p] >= b[1]) p--;
		swap(b[1], b[p]);
		downAdjust(b, 1, p - 1);
	}
	for (int i = 1; i <= n; i++) {
		if (i != 1)
			printf(" ");
		printf("%d", b[i]);
	}

	return 0;

}

 

Windows平台高效BLE调试工具开发指南 本文为开发者提供了一份在Windows平台上开发高效BLE调试工具的详细指南。文章深入探讨了使用32feet.NET库进行蓝牙低功耗设备扫描、连接、数据通信及进阶功能开发的实战经验,旨在帮助开发者打造功能自定义、性能优化的专属调试利器,显著提升物联网和嵌入式设备的调试效率。 阅读详情

相关推荐

从PSDK到ROS节点:实战GPS数据发布与CMake配置

本文详细介绍了如何从大疆PSDK获取GPS数据并封装为ROS节点的实战过程,包括PSDK编译、GPS数据订阅、ROS节点实现及CMake配置优化。通过具体代码示例和调试技巧,帮助开发者快速实现无人机传感器数据与ROS系统的集成,提升开发效率。

weixin_30843605的博客 458

1089. Insert or Merge (25)-PAT甲级真题

1089. Insert or Merge (25) According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort remove

柳婼 の blog 3750

立创EDA开源足轮机器人:基于STM32F407与ESP32的LQR自平衡与FOC驱动设计全解析

本文详细解析了基于立创EDA平台的开源足轮机器人项目。该项目采用STM32F407作为主控,ESP32负责无线通信,并应用LQR自平衡算法与FOC驱动技术,实现了机器人的稳定站立、移动转向及复杂地形适应能力。文章深入剖析了其硬件架构、软件设计及调试经验,为机器人运动控制学习提供了宝贵参考。

weixin_42608299的博客 232

PAT甲级刷题记录——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, ...

一只奋斗中的本科生 453

手把手教学:利用AutoDL与vLLM快速部署DeepSeek-R1大模型实战指南

本文详细介绍了如何利用AutoDL平台和vLLM框架快速部署DeepSeek-R1大模型的实战指南。从服务器配置、vLLM安装优化到模型部署的两种方案,再到性能调优和生产环境建议,手把手教你高效完成大模型部署。特别针对国内开发者遇到的网络问题提供了稳定解决方案,并分享了显存优化、磁盘清理等实用技巧。

weixin_42531779的博客 330

【180609】剑侠情缘_整套源码+地图编辑器(单机学习例子)

剑侠情缘_整套源码+地图编辑编(单机学习例子)完整源码下载地址:点击下载备用下载地址:点击下载

资源小站 6278

1098 Insertion or Heap Sort(堆排序解释)(PAT甲级)

堆排序是对选择排序的改进,利用数据结构来排序。1.编写向下过滤函数percDown2.建立最大堆3.每次将堆顶(即最大值)与最后一个元素交换,然后“抛弃”最后一个元素,知道将最大堆转换成升序数组我们根据父节点和子节点的大小关系来判断是否需要交换父子结点,使得满足最大堆序。由于最大堆一定是完全二叉树,所以我们可以用数组存储数据。由于需要满足每个父节点都比起子节点大,自然的想到,从最后一个父节点开始做判断。那么传入的第一个父节点的下标就是(len / 2 - 1) 其中len是数组长度。 如上图所示,我们找到

看见什么心乱 733

PTGui使用教程

PTGui使用教程为啥使用ptgui开始使用ptgui 为啥使用ptgui ptgui是一个全景拼接软件,在海思hi3559上做视频拼接的时候,需要拼接参数,这是就需要使用到ptgui,这里可以在该网站下下载ptgui:下载地址 开始使用ptgui 第一步,加载图片,我这里添加了三幅图 然后添加控制点(可以先多对准几次图像,这样会自动生成一些控制点,但是不保证准确),建议控制点不要太密集 点开镜头设置,勾选以下几个选项以及配置好镜头类型,如下图 点开优化器,在高级界面中配置如下参数,最后点

what_wrong的博客 1万+

【PAT刷题甲级】1089.Insert or Merge

1089 Insert or Merge (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 belong

C_greenbird的博客 437

SAP_MM模块非限制库存转冻结库存

项目非限制 → 冻结事务码移动类型343影响库存不可用,不参与MRP常见用途质量问题封存、盘点冻结、待退货品通过本文,我们了解了 SAP 的[SAP_MM模块非限制库存转冻结库存]的基本概念和应用方法。希望这些信息能帮助您在实际工作中更好地使用 SAP 系统,提高工作效率和管理水平。更多SAP文章请点击更多SAP文章更新,大家一起学习进步!!!文章中如有错误。敬请指正!搜索公众号:SAP资料文库微信号:SPRO_PP时间:2025-10-14。

我小时候很黑的博客 588

Qualcomm AI Engine Direct SDK(QNN)的使用介绍

Qualcomm AI Engine Direct SDK,高通新的AI模型推理框架,又名QNN,为AI开发提供统一API的底层框架。通过这个SDK,可以在高通的 CPU/GPU和HTP上对AI模型进行性能加速。2.Qualcomm AI Engine Direct SDK的公开英文文档。1.Qualcomm AI Engine Direct SDK的下载和安装。

u013250861的博客 6974

PAT (Advanced Level) Practice 1098 Insertion or Heap Sort (25 分)

Insertion or Heap Sort 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...

就当一次路过丶的沙滩 386

YOLOv9 损失函数解析

YOLOv9作为YOLO系列的最新成员,在损失函数设计上进行了多项创新,重点优化了动态任务对齐机制、多模态定位损失融合以及跨层级梯度协同,进一步提升了检测精度与训练效率。

浩瀚之水的专栏 1293

1098. Insertion or Heap Sort (25)-PAT甲级真题(堆排序)

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...

柳婼 の blog 5576

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)

1098Insertion or Heap Sort(25分) According to Wikipedia: Insertion sortiterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort remo...

小熊猫的博客 504

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

PAT甲级1098 Insertion or Heap Sort (25分) 堆排序 swap交换数组元素

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

qq_32691667的博客 168

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

PAT甲级1098 Insertion or Heap Sort (25 分)题解

\quad插入排序和堆排序,排序算法的详解请移步博客 #include <iostream> #include <vector> #include <algorithm> using namespace std; void output(vector<int> &a) { for (int i = 1; i < a.size();...

qq_40438165的博客 1521

1098 Insertion or Heap Sort (PAT甲级)

已经根据柳婼的解法改进了一些,但她的代码还是简洁不少。

linh2006的博客 520

【PAT甲级 - C++题解】1098 Insertion or Heap Sort

📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!

Newin2020的博客 484

PAT 甲级 1098  Insertion or Heap Sort

1098 Insertion or Heap Sort (25 point(s)) According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion so...

漫浸天空的雨色的备忘录~ 826

1098. Insertion or Heap Sort (25)堆排序

1098. Insertion or Heap Sort (25) 时间限制1000 ms内存限制65536 KB代码长度限制100 KB判断程序Standard(来自小小) 题目描述 According to Wikipedia:Insertion sort iterates, consuming one input element each r...

weixin_30706507的博客 119

【PAT甲级】1098 Insertion or Heap Sort (25 分)

PAT甲级1098题解 第4个测试点未通过

小染的学习笔记 1991

PAT甲级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,

R11happy的博客 639

【PAT甲级】1098 Insertion or Heap Sort (25分)

解题过程的小记录,如有错误欢迎指出。 难度:五星(关于堆的模板要会背默) 小导航~题目分析注意点我的解题过程思路bug代码dalao的代码借鉴点 题目分析 给出一串数列的原始数列,和中间排序数列,判断原始数列数通过插入排序还是堆排序达到中间数列,并给出下一状态的数列 注意点 原始数列不参与比较(因为会出现有前两位刚好是递增的情况,如果算入原始数列,那么 第一次和第二次插入排序的都是一样的数列,对于下一步数列的输出就会有歧义) 我的解题过程 思路 用vector数组保存原始数列,中间数列,以及经过插入排序变

SpicyChicken的自我修养 271

PAT甲级1098 Insertion or Heap Sort (25 分)

1098 Insertion or Heap Sort (25 分) According to Wikipedia: Insertion sortiterates, 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 locatio...

weixin_44847698的博客 719

PAT甲级A1098 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,...

vir_lee的博客 301
上一篇: PAT甲级 - 1107 Social Clusters (30 分) 并查集
下一篇: PAT甲级 -1013 Battle Over Cities (25分) 求连通块的数量
DedicateToAI
博客等级 码龄9年 6粉丝 90原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值