博客
关于我
程序设计入门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/

你可能感兴趣的文章
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>
MySQL中地理位置数据扩展geometry的使用心得
查看>>
Mysql中存储引擎简介、修改、查询、选择
查看>>
Mysql中存储过程、存储函数、自定义函数、变量、流程控制语句、光标/游标、定义条件和处理程序的使用示例
查看>>
mysql中实现rownum,对结果进行排序
查看>>
mysql中对于数据库的基本操作
查看>>
Mysql中常用函数的使用示例
查看>>
MySql中怎样使用case-when实现判断查询结果返回
查看>>
Mysql中怎样使用update更新某列的数据减去指定值
查看>>
Mysql中怎样设置指定ip远程访问连接
查看>>
mysql中数据表的基本操作很难嘛,由这个实验来带你从头走一遍
查看>>
Mysql中文乱码问题完美解决方案
查看>>
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>
Mysql中的 IFNULL 函数的详解
查看>>
mysql中的collate关键字是什么意思?
查看>>
MySql中的concat()相关函数
查看>>
mysql中的concat函数,concat_ws函数,concat_group函数之间的区别
查看>>