九、排序(下):Insertion or Heap Sort

2023-12-30 14:59
文章标签 heap 排序 sort insertion

本文主要是介绍九、排序(下):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 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<stdio.h>#define MaxNum 100void Insertion_Next_Iteration(int B[], int i, int N)
{int j, temp = B[i];for( j=i-1; j>=0; --j ){if( B[j]<=temp ){B[j+1] = temp;break;}	else B[j+1] = B[j];}if(B[0]>temp)B[0]=temp;for( j=0; j!=N-1; ++j )printf("%d ",B[j]);printf("%d",B[N-1]);
}int Heap_N(int B[], int N)
{/* 得到剩下的最大堆的最后一个结点 */int i, S=0;for( i=N-1; i>=1; --i )if( B[i]<B[i-1] || B[i]<B[0] ){S = 1;break;}if( S==1 )return i;else return 0;
}void RebuildMax(int B[], int n)
{int temp = B[0];int Parent, Child;for( Parent=0; Parent*2+1<=n-1; Parent=Child ){Child = Parent*2+1;if(Child<n-1 && B[Child]<B[Child+1])++Child;if(temp>=B[Child])break;elseB[Parent] = B[Child];}B[Parent] = temp;
}void Heap_Next_Iteration(int A[], int B[], int N)
{int n = Heap_N(B, N);int temp = B[n];B[n] = B[0];B[0] = temp;RebuildMax(B, n);int j;for( j=0; j<N-1; ++j )printf("%d ",B[j]);printf("%d",B[N-1]);
}void Judge(int A[], int B[], int N)
{int i, j, name, S=1;for( i=1; i!=N; ++i )if( B[i]<B[i-1] ) /* 找到有序序列的末尾i-1 */break;for( j=i; j!=N; ++j )if( A[j]!=B[j] ){ /* 看后面的序列有没有进行过操作 */S = 0;break;}if( S ){ /* 后面的序列没动过,说明对前i-1个进行了插入排序 */printf("Insertion Sort\n");Insertion_Next_Iteration(B,i,N);}else{ /* 否则为堆排序 */printf("Heap Sort\n");Heap_Next_Iteration(A,B,N);}}int main()
{int i, N, A[MaxNum], B[MaxNum];scanf("%d\n",&N);for( i=0; i!=N; ++i )scanf("%d",&A[i]);for( i=0; i!=N; ++i )scanf("%d",&B[i]);Judge(A,B,N);return 0;
}

解题思路

和上一道题Insert or Merge基本相同,先判断是不是Insert_Sort,否则就是Heap_Sort。Heaap_Sort再进行一步迭代就是取出最大堆堆顶元素和堆最后元素交换位置,然后再重新调整成最大堆。

这篇关于九、排序(下):Insertion or Heap Sort的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/553214

相关文章

Java List排序实例代码详解

《JavaList排序实例代码详解》:本文主要介绍JavaList排序的相关资料,Java排序方法包括自然排序、自定义排序、Lambda简化及多条件排序,实现灵活且代码简洁,文中通过代码介绍的... 目录一、自然排序二、自定义排序规则三、使用 Lambda 表达式简化 Comparator四、多条件排序五、

JAVA数组中五种常见排序方法整理汇总

《JAVA数组中五种常见排序方法整理汇总》本文给大家分享五种常用的Java数组排序方法整理,每种方法结合示例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录前言:法一:Arrays.sort()法二:冒泡排序法三:选择排序法四:反转排序法五:直接插入排序前言:几种常用的Java数组排序

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

idea maven编译报错Java heap space的解决方法

《ideamaven编译报错Javaheapspace的解决方法》这篇文章主要为大家详细介绍了ideamaven编译报错Javaheapspace的相关解决方法,文中的示例代码讲解详细,感兴趣的... 目录1.增加 Maven 编译的堆内存2. 增加 IntelliJ IDEA 的堆内存3. 优化 Mave

Mybatis 传参与排序模糊查询功能实现

《Mybatis传参与排序模糊查询功能实现》:本文主要介绍Mybatis传参与排序模糊查询功能实现,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、#{ }和${ }传参的区别二、排序三、like查询四、数据库连接池五、mysql 开发企业规范一、#{ }和${ }传参的

C++快速排序超详细讲解

《C++快速排序超详细讲解》快速排序是一种高效的排序算法,通过分治法将数组划分为两部分,递归排序,直到整个数组有序,通过代码解析和示例,详细解释了快速排序的工作原理和实现过程,需要的朋友可以参考下... 目录一、快速排序原理二、快速排序标准代码三、代码解析四、使用while循环的快速排序1.代码代码1.由快

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

大数据小内存排序问题如何巧妙解决

《大数据小内存排序问题如何巧妙解决》文章介绍了大数据小内存排序的三种方法:数据库排序、分治法和位图法,数据库排序简单但速度慢,对设备要求高;分治法高效但实现复杂;位图法可读性差,但存储空间受限... 目录三种方法:方法概要数据库排序(http://www.chinasem.cn对数据库设备要求较高)分治法(常

Python中lambda排序的六种方法

《Python中lambda排序的六种方法》本文主要介绍了Python中使用lambda函数进行排序的六种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录1.对单个变量进行排序2. 对多个变量进行排序3. 降序排列4. 单独降序1.对单个变量进行排序

关于Java内存访问重排序的研究

《关于Java内存访问重排序的研究》文章主要介绍了重排序现象及其在多线程编程中的影响,包括内存可见性问题和Java内存模型中对重排序的规则... 目录什么是重排序重排序图解重排序实验as-if-serial语义内存访问重排序与内存可见性内存访问重排序与Java内存模型重排序示意表内存屏障内存屏障示意表Int