博客
关于我
程序设计入门21 堆排序与插入排序
阅读量:391 次
发布时间:2019-03-05

本文共 3927 字,大约阅读时间需要 13 分钟。

1098 Insertion or Heap Sort (25 分)

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 resuling 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:

103 1 2 8 7 5 9 4 6 01 2 3 7 8 5 9 4 6 0

Sample Output 1:

Insertion Sort1 2 3 5 7 8 9 4 6 0

Sample Input 2:

103 1 2 8 7 5 9 4 6 06 4 5 1 0 3 2 7 8 9

Sample Output 2:

Heap Sort5 4 3 1 0 2 6 7 8 9

一,主要思想

    堆排序到底是从什么时候开始比较?是在生成初始化堆之后,每一次确定最大元素的那个for中进行每一轮的比较,而不是在堆排序的迭代中比较。

二,注意点

1,本题的这句话, together with a sequence which is a result of several iterations of some sorting method,说明比较应当排除初始序列,因为给出的序列是经过数轮迭代之后的

2,下次尽量把代码写得模块化,判断函数isSame(int A[],B[])与输出函数print(int A[])分开去写。

三,正确代码

#include
#include
using namespace std;const int max_n = 110;int arr[max_n];int arr_1[max_n];int arr_2[max_n];int target[max_n];int N = 0;int flag_1 = 0;void insert_sort() { int flag = 0; for (int i = 2; i <= N; i++) { for (int j = 1; j <= N; j++) { if (arr_1[j] != target[j]||i==2) { flag = 1; break; } } int temp = arr_1[i], j = i; //尤其注意前面那个j>1,有效避免了若是需要在第一位插入时的特殊复杂情况。 while (j > 1 && temp < arr_1[j - 1]) { arr_1[j] = arr_1[j - 1]; j--; } arr_1[j] = temp; if (flag == 1) { flag = 0; } else { flag_1 = 1; printf("Insertion Sort\n"); return; } }}void heap_insert(int low, int hign) { int i = low, j = i * 2; while (j <= hign) { if (j + 1 <= hign&&arr_2[j + 1] > arr_2[j]) { j = j + 1; } if (arr_2[i] < arr_2[j]) { swap(arr_2[i], arr_2[j]); /* 这是错的,因为这是自下而上,与前面矛盾 j = i; i = j / 2; */ i = j; j = i * 2; } else { break; } }}void create_heap() { for (int i = N / 2; i >= 1; i--) { heap_insert(i, N); }}int main() { scanf("%d", &N); for (int i = 1; i <= N; i++) { scanf("%d", &arr[i]); } for (int i = 1; i <= N; i++) { scanf("%d", &target[i]); } for (int i = 1; i <= N; i++) { arr_1[i] = arr[i]; } insert_sort(); if (flag_1 == 1) { for (int i = 1; i <= N; i++) { printf("%d", arr_1[i]); if (i != N) { printf(" "); } } } else { for (int i = 1; i <= N; i++) { arr_2[i] = arr[i]; } create_heap(); for (int i = N; i > 1; i--) { int flag = 0; for (int j = 1; j <= N; j++) { if (arr_2[j] != target[j]||i==N) { flag = 1; break; } } swap(arr_2[1], arr_2[i]); heap_insert(1, i - 1); if (flag == 1) { flag = 0; } else { printf("Heap Sort\n"); for (int k = 1; k <= N; k++) { printf("%d", arr_2[k]); if (k != N) { printf(" "); } } return 0; } } } return 0;}

 

转载地址:http://bolwz.baihongyu.com/

你可能感兴趣的文章
MySQL5.7.37windows解压版的安装使用
查看>>
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>