博客
关于我
程序设计入门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 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>
mysql 添加索引
查看>>
MySQL 添加索引,删除索引及其用法
查看>>
MySQL 用 limit 为什么会影响性能?
查看>>
MySQL 用 limit 为什么会影响性能?有什么优化方案?
查看>>