九、排序(下):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

相关文章

Python中的sort()和sorted()用法示例解析

《Python中的sort()和sorted()用法示例解析》本文给大家介绍Python中list.sort()和sorted()的使用区别,详细介绍其参数功能及Timsort排序算法特性,涵盖自适应... 目录一、list.sort()参数说明常用内置函数基本用法示例自定义函数示例lambda表达式示例o

C++归并排序代码实现示例代码

《C++归并排序代码实现示例代码》归并排序将待排序数组分成两个子数组,分别对这两个子数组进行排序,然后将排序好的子数组合并,得到排序后的数组,:本文主要介绍C++归并排序代码实现的相关资料,需要的... 目录1 算法核心思想2 代码实现3 算法时间复杂度1 算法核心思想归并排序是一种高效的排序方式,需要用

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam

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