算法 排序3 Insertion or Heap Sort

2024-04-07 20:48

本文主要是介绍算法 排序3 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 <iostream>
using namespace std;#define MaxVertexNum 105   //最大100个数据,多留出5个空白
typedef int ElementType;
ElementType myArray[MaxVertexNum];//存原数据,插入排序用
ElementType myArray_1[MaxVertexNum];//存原数据,归并排序用
ElementType myArray2[MaxVertexNum];//存第二行数据int isInsertFlag = 0;
bool isSame(ElementType A[], ElementType B[], int N) {for (int i = 0;i < N;i++) {if (A[i] != B[i])return false;}return true;
}void InsertionSort(ElementType A[], int N)
{ // 插入排序 int P, i;ElementType Tmp;int flag = 0;for (P = 1; P<N; P++) {Tmp = A[P]; // 取出未排序序列中的第一个元素for (i = P; i>0 && A[i - 1]>Tmp; i--)A[i] = A[i - 1]; //依次与已排序序列中元素比较并右移A[i] = Tmp; // 放进合适的位置 if (flag == 1)break;if (isSame(A, myArray2, N)) {flag = 1;isInsertFlag = 1;cout << "Insertion Sort"<<endl;}}
}void Swap(ElementType *a, ElementType *b)
{ElementType t = *a; *a = *b; *b = t;
}void PercDown(ElementType A[], int p, int N)
{ // 改编代码4.24的PercDown( MaxHeap H, int p )    // 将N个元素的数组中以A[p]为根的子堆调整为最大堆 int Parent, Child;ElementType X;X = A[p]; // 取出根结点存放的值 for (Parent = p; (Parent * 2 + 1)<N; Parent = Child) {Child = Parent * 2 + 1;if ((Child != N - 1) && (A[Child]<A[Child + 1]))Child++;  // Child指向左右子结点的较大者 if (X >= A[Child]) break; // 找到了合适位置 else  // 下滤X A[Parent] = A[Child];}A[Parent] = X;
}void HeapSort(ElementType A[], int N)
{ // 堆排序 int i;int flag = 0;for (i = N / 2 - 1; i >= 0; i--)// 建立最大堆 PercDown(A, i, N);for (i = N - 1; i>0; i--) {// 删除最大堆顶 Swap(&A[0], &A[i]);PercDown(A, 0, i);if (flag == 1)break;if (isSame(A, myArray2, N)) {flag = 1;}}
}int main(void) {int N;cin >> N;for (int i = 0;i < N;i++) {cin >> myArray[i];myArray_1[i] = myArray[i];}for (int i = 0;i < N;i++)cin >> myArray2[i];InsertionSort(myArray, N);//插入排序if (isInsertFlag) {	for (int i = 0;i < N - 1;i++)cout << myArray[i] << " ";cout << myArray[N - 1];}	else if (!isInsertFlag) {cout << "Heap Sort" << endl;HeapSort(myArray_1, N);for (int i = 0;i < N - 1;i++)cout << myArray_1[i] << " ";cout << myArray_1[N - 1];}system("pause");return 0;
}

测试结果:

这篇关于算法 排序3 Insertion or Heap Sort的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

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

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

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

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

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

一文详解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数组排序

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.

Springboot实现推荐系统的协同过滤算法

《Springboot实现推荐系统的协同过滤算法》协同过滤算法是一种在推荐系统中广泛使用的算法,用于预测用户对物品(如商品、电影、音乐等)的偏好,从而实现个性化推荐,下面给大家介绍Springboot... 目录前言基本原理 算法分类 计算方法应用场景 代码实现 前言协同过滤算法(Collaborativ

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

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

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