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

相关文章

Go语言中泄漏缓冲区的问题解决

《Go语言中泄漏缓冲区的问题解决》缓冲区是一种常见的数据结构,常被用于在不同的并发单元之间传递数据,然而,若缓冲区使用不当,就可能引发泄漏缓冲区问题,本文就来介绍一下问题的解决,感兴趣的可以了解一下... 目录引言泄漏缓冲区的基本概念代码示例:泄漏缓冲区的产生项目场景:Web 服务器中的请求缓冲场景描述代码

Go语言如何判断两张图片的相似度

《Go语言如何判断两张图片的相似度》这篇文章主要为大家详细介绍了Go语言如何中实现判断两张图片的相似度的两种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 在介绍技术细节前,我们先来看看图片对比在哪些场景下可以用得到:图片去重:自动删除重复图片,为存储空间"瘦身"。想象你是一个

Go语言中Recover机制的使用

《Go语言中Recover机制的使用》Go语言的recover机制通过defer函数捕获panic,实现异常恢复与程序稳定性,具有一定的参考价值,感兴趣的可以了解一下... 目录引言Recover 的基本概念基本代码示例简单的 Recover 示例嵌套函数中的 Recover项目场景中的应用Web 服务器中

Go语言中使用JWT进行身份验证的几种方式

《Go语言中使用JWT进行身份验证的几种方式》本文主要介绍了Go语言中使用JWT进行身份验证的几种方式,包括dgrijalva/jwt-go、golang-jwt/jwt、lestrrat-go/jw... 目录简介1. github.com/dgrijalva/jwt-go安装:使用示例:解释:2. gi

Go 语言中的 Struct Tag 的用法详解

《Go语言中的StructTag的用法详解》在Go语言中,结构体字段标签(StructTag)是一种用于给字段添加元信息(metadata)的机制,常用于序列化(如JSON、XML)、ORM映... 目录一、结构体标签的基本语法二、json:"token"的具体含义三、常见的标签格式变体四、使用示例五、使用

Go语言使用slices包轻松实现排序功能

《Go语言使用slices包轻松实现排序功能》在Go语言开发中,对数据进行排序是常见的需求,Go1.18版本引入的slices包提供了简洁高效的排序解决方案,支持内置类型和用户自定义类型的排序操作,本... 目录一、内置类型排序:字符串与整数的应用1. 字符串切片排序2. 整数切片排序二、检查切片排序状态:

基于Go语言实现Base62编码的三种方式以及对比分析

《基于Go语言实现Base62编码的三种方式以及对比分析》Base62编码是一种在字符编码中使用62个字符的编码方式,在计算机科学中,,Go语言是一种静态类型、编译型语言,它由Google开发并开源,... 目录一、标准库现状与解决方案1. 标准库对比表2. 解决方案完整实现代码(含边界处理)二、关键实现细

如何合理管控Java语言的异常

《如何合理管控Java语言的异常》:本文主要介绍如何合理管控Java语言的异常问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍2、Thorwable类3、Error4、Exception类4.1、检查异常4.2、运行时异常5、处理方式5.1. 捕获异常

C语言中的常见进制转换详解(从二进制到十六进制)

《C语言中的常见进制转换详解(从二进制到十六进制)》进制转换是计算机编程中的一个常见任务,特别是在处理低级别的数据操作时,C语言作为一门底层编程语言,在进制转换方面提供了灵活的操作方式,今天,我们将深... 目录1、进制基础2、C语言中的进制转换2.1 从十进制转换为其他进制十进制转二进制十进制转八进制十进

$在R语言中的作用示例小结

《$在R语言中的作用示例小结》在R语言中,$是一个非常重要的操作符,主要用于访问对象的成员或组件,它的用途非常广泛,不仅限于数据框(dataframe),还可以用于列表(list)、环境(enviro... 目录1. 访问数据框(data frame)中的列2. 访问列表(list)中的元素3. 访问jav