算法 排序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

相关文章

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函数排序

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

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

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

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

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

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.