Insertion or Heap Sort (25分)【C语言】

2024-04-23 16:38
文章标签 语言 25 heap sort insertion

本文主要是介绍Insertion or Heap Sort (25分)【C语言】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

  • 题目:
    • 输入格式
    • 输出格式
    • 输入样例
    • 输出样例
    • 输入样例
    • 输出样例
  • 算法
    • 问题分析
      • 代码实现
        • HeapSort函数:一趟堆排序
        • PerDown、BuildMaxHeap函数:下滤和生成最大堆
        • IsInsertion函数:判断是否是插入函数

题目:

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?

输入格式

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.

输出格式

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.

输入样例

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

输出样例

Insertion Sort
1 2 3 5 7 8 9 4 6 0

输入样例

10
3 1 2 8 7 5 9 4 6 0
6 4 5 1 0 3 2 7 8 9

输出样例

Heap Sort
5 4 3 1 0 2 6 7 8 9

算法

问题分析

  • 判断是否是插入排序
    在这里插入图片描述
  • 如何进行下一步堆排序

用原始数据,一次一次生成堆排序序列,和已经存在的队列对比。当发现符合时,再进行一步。

代码实现

typedef int ElementType;
int main()
{	int N;scanf("%d",&N);ElementType * A=(ElementType*)malloc(sizeof(ElementType)*N);ElementType * B=(ElementType*)malloc(sizeof(ElementType)*N);int i;for(i=0;i<N;i++){scanf("%d",&(A[i]));}for(i=0;i<N;i++){scanf("%d",&(B[i]));}int Is=IsInsertion(A,B,N);if(Is>1){printf("Insertion Sort\n");int Temp=B[Is];for(i=Is;i>0&&(B[i-1]>Temp);i--){B[i]=B[i-1];}B[i]=Temp;}else{printf("Heap Sort\n");int flag=0;int t=N;do{HeapSort(A,t);for(i=0;i<N;i++){if(A[i]!=B[i]){t--;break;}if(i==N-1){flag=1;}}}while(flag==0);HeapSort(A,t-1);for(i=0;i<N;i++){B[i]=A[i];}}for(i=0;i<N;i++){printf("%d",B[i]);if(i!=N-1){printf(" ");}}return 0;
}
HeapSort函数:一趟堆排序
void HeapSort(ElementType A[],int N)
{static flag=0;if(flag==0){BuildMaxHeap(A,N);flag=1; }ElementType T=A[0];A[0]=A[N-1];A[N-1]=T;PerDown(A,N-1,0);
}
PerDown、BuildMaxHeap函数:下滤和生成最大堆
void PerDown(ElementType A[],int N,int Add)
{//child=2*parent+1; ElementType T=A[Add];int parent=Add;int child;for(;parent*2+1<=N-1;parent=child){child=parent*2+1;if(child<N-1&&A[child+1]>A[child]){child+=1;}if(A[child]<=T){break;};A[parent]=A[child];}A[parent]=T;
}
void BuildMaxHeap(ElementType A[],int N)
{int i;for(i=N/2-1;i>-1;i--){PerDown(A,N,i);}
}		
IsInsertion函数:判断是否是插入函数
int IsInsertion(ElementType A[],ElementType B[],int N)
{int i;int ret=0;for(i=1;i<N;i++){if(B[i]<B[i-1]){ret=i;break;}}for(i=ret;i<N;i++){if(A[i]!=B[i]){ret=0;}}return ret;
} 

这篇关于Insertion or Heap Sort (25分)【C语言】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/929330

相关文章

C语言中的数据类型强制转换

《C语言中的数据类型强制转换》:本文主要介绍C语言中的数据类型强制转换方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C语言数据类型强制转换自动转换强制转换类型总结C语言数据类型强制转换强制类型转换:是通过类型转换运算来实现的,主要的数据类型转换分为自动转换

利用Go语言开发文件操作工具轻松处理所有文件

《利用Go语言开发文件操作工具轻松处理所有文件》在后端开发中,文件操作是一个非常常见但又容易出错的场景,本文小编要向大家介绍一个强大的Go语言文件操作工具库,它能帮你轻松处理各种文件操作场景... 目录为什么需要这个工具?核心功能详解1. 文件/目录存javascript在性检查2. 批量创建目录3. 文件

C语言实现两个变量值交换的三种方式

《C语言实现两个变量值交换的三种方式》两个变量值的交换是编程中最常见的问题之一,以下将介绍三种变量的交换方式,其中第一种方式是最常用也是最实用的,后两种方式一般只在特殊限制下使用,需要的朋友可以参考下... 目录1.使用临时变量(推荐)2.相加和相减的方式(值较大时可能丢失数据)3.按位异或运算1.使用临时

使用C语言实现交换整数的奇数位和偶数位

《使用C语言实现交换整数的奇数位和偶数位》在C语言中,要交换一个整数的二进制位中的奇数位和偶数位,重点需要理解位操作,当我们谈论二进制位的奇数位和偶数位时,我们是指从右到左数的位置,本文给大家介绍了使... 目录一、问题描述二、解决思路三、函数实现四、宏实现五、总结一、问题描述使用C语言代码实现:将一个整

C语言字符函数和字符串函数示例详解

《C语言字符函数和字符串函数示例详解》本文详细介绍了C语言中字符分类函数、字符转换函数及字符串操作函数的使用方法,并通过示例代码展示了如何实现这些功能,通过这些内容,读者可以深入理解并掌握C语言中的字... 目录一、字符分类函数二、字符转换函数三、strlen的使用和模拟实现3.1strlen函数3.2st

Go语言中最便捷的http请求包resty的使用详解

《Go语言中最便捷的http请求包resty的使用详解》go语言虽然自身就有net/http包,但是说实话用起来没那么好用,resty包是go语言中一个非常受欢迎的http请求处理包,下面我们一起来学... 目录安装一、一个简单的get二、带查询参数三、设置请求头、body四、设置表单数据五、处理响应六、超

C语言中的浮点数存储详解

《C语言中的浮点数存储详解》:本文主要介绍C语言中的浮点数存储详解,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、首先明确一个概念2、接下来,讲解C语言中浮点型数存储的规则2.1、可以将上述公式分为两部分来看2.2、问:十进制小数0.5该如何存储?2.3 浮点

基于Python实现多语言朗读与单词选择测验

《基于Python实现多语言朗读与单词选择测验》在数字化教育日益普及的今天,开发一款能够支持多语言朗读和单词选择测验的程序,对于语言学习者来说无疑是一个巨大的福音,下面我们就来用Python实现一个这... 目录一、项目概述二、环境准备三、实现朗读功能四、实现单词选择测验五、创建图形用户界面六、运行程序七、

使用Go语言开发一个命令行文件管理工具

《使用Go语言开发一个命令行文件管理工具》这篇文章主要为大家详细介绍了如何使用Go语言开发一款命令行文件管理工具,支持批量重命名,删除,创建,移动文件,需要的小伙伴可以了解下... 目录一、工具功能一览二、核心代码解析1. 主程序结构2. 批量重命名3. 批量删除4. 创建文件/目录5. 批量移动三、如何安

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本