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

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

题目大意:给你一组初始序列和中间序列,让你根据序列来判断是插入排序还是堆排序。然后输出中间序列的下一趟排序

解法1:按照题目所给要求对插入排序的每一步进行模拟,每一趟的排序结果和中间序列进行比较,如果相同则为插入排序,否则为堆排序。注意:因为序列是按照从小到大排列,所以根据堆的性质我们要构建的堆是大根堆。

代码:

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;
int init_array[N], final_array[N], tmp_array[N];
int n; bool same_array() {
for (int i = 1; i <= n; i++) {
if (init_array[i] != final_array[i]) return false;
}
return true;
} void print_array() {
for (int i = 1; i <= n; i++) {
cout << init_array[i];
if (i != n)
cout << " ";
else
cout << endl;
}
} bool insert_sort() {
int i, j;
bool flag = false;
for (i = 2; i <= n; i++) {
if (i != 2 && same_array()) flag = true;
if (init_array[i] < init_array[i - 1]) {
init_array[0] = init_array[i];
for (j = i - 1; init_array[0] < init_array[j]; j--)
init_array[j + 1] = init_array[j];
init_array[j + 1] = init_array[0];
if (flag) {
return true;
}
}
}
return false;
} void down(int u, int sizes) {
int t = u;
if (u * 2 <= sizes && init_array[u * 2] > init_array[t]) t = u * 2;
if (u * 2 + 1 <= sizes && init_array[u * 2 + 1] > init_array[t])
t = u * 2 + 1;
if (t != u) {
swap(init_array[t], init_array[u]);
down(t, sizes);
}
} void up(int u) {
while (u / 2 && init_array[u / 2] > init_array[u]) {
swap(init_array[u / 2], init_array[u]);
u /= 2;
}
} void heap_sort() {
//初建堆,因为是从小打到输出所以构建的是大顶堆
for (int i = n / 2; i >= 1; i--) down(i, n);
bool flag = false;
for (int i = n; i > 1; i--) {
if (i != n && same_array()) flag = true;
swap(init_array[1], init_array[i]);
down(1, i - 1);
if (flag) {
print_array();
return;
}
}
} int main() {
scanf("%d", &n);
// sizes = n;
for (int i = 1; i <= n; i++) {
scanf("%d", &init_array[i]);
tmp_array[i] = init_array[i];
}
for (int i = 1; i <= n; i++) scanf("%d", &final_array[i]);
if (insert_sort()) {
puts("Insertion Sort");
print_array();
} else {
puts("Heap Sort");
for (int i = 1; i <= n; i++) init_array[i] = tmp_array[i];
heap_sort();
}
return 0;
}

解法2:参考柳神的解法 ,堆排序大致思路相同,关键的是对插入排序的优化。根据插入排序的性质可以知道结点L(i)前L(1 ~i - 1)个元素都是有序序列,结点L(i + 1, n)个元素都是无序序列。所以我们只需要在中间序列中找到结点L(i)然后比较中间序列的L(i + 1, n)和原序列的L(i + 1, n),如果相同则为插入排序,如果不同则为堆排序。如果是插入排序直接对L(1 ~ i + 1)个元素进行sort即可。

利用插入排序优化的代码如下:

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;
int init_array[N], final_array[N];
int n; void down(int h[], int u, int sizes) {
int t = u;
if (u * 2 <= sizes && h[u * 2] > h[t]) t = u * 2;
if (u * 2 + 1 <= sizes && h[u * 2 + 1] > h[t])
t = u * 2 + 1;
if (t != u) {
swap(h[t], h[u]);
down(h, t, sizes);
}
} int main() {
scanf("%d", &n);
// sizes = n;
for (int i = 1; i <= n; i++) {
scanf("%d", &init_array[i]);
}
for (int i = 1; i <= n; i++) scanf("%d", &final_array[i]);
int p = 1;
while(p < n && final_array[p] <= final_array[p + 1]) p++;
bool flag = true;
for (int i = p + 1; i <= n; i++) {
if (init_array[i] != final_array[i]) {
flag = false;
break;
}
}
if (flag) {
puts("Insertion Sort");
sort(final_array + 1, final_array + p + 2);
}
else {
puts("Heap Sort");
int index = n;
while(index > 2 && final_array[index] > final_array[1]) index--;
swap(final_array[1], final_array[index]);
down(final_array,1, index - 1);
}
for (int i = 1; i <= n; i++) {
cout << final_array[i];
if (i != n)
cout << " ";
else
cout << endl;
}
return 0;
}

最新文章

  1. Drupal 8.2.4安装简体中文步骤
  2. NOIP2016滚粗计
  3. MyCAT实现MySQL的读写分离
  4. Spring Boot 添加Shiro支持
  5. linux下打开、关闭tomcat,实时查看tomcat运行日志
  6. 用户 NT AUTHORITY\NETWORK SERVICE 登录失败 解决方法(转载)
  7. 解决Excel 2010只有一个窗口的问题
  8. UE4在C++中使用OnComponentBeginOverlap之类的时间
  9. IQ推理:红眼睛和蓝眼睛
  10. 基础套接字的C#网络编程
  11. WPF MultiBinding 和 IMultiValueConverter
  12. Java基础知识强化之多线程笔记04:并行和并发 区别
  13. WebWork2和Spring MVC Framework的比较
  14. Spark机器学习笔记一
  15. Go代理,修改标题
  16. ORACLE查询语句
  17. php基础的第一天 任务点滴,event对象方法概括 ing....
  18. AngularJs学习笔记2-控制器、数据绑定、作用域
  19. java对象与json对象之间的转换
  20. springmvc 文件上传(粘贴即用)

热门文章

  1. web.xml启动时调用java类方法
  2. SQL系列总结——基础篇(三)
  3. Malaysia Trip Memory (&#39;-ωก)
  4. 2020牛客暑期多校训练营(第一场)Easy Integration
  5. AtCoder Beginner Contest 168
  6. 【bzoj 2597】[Wc2007]剪刀石头布(图论--网络流 最小费用最大流)
  7. 数据库之ODPS中sql语句指南
  8. leetcode11 盛水容器 贪心
  9. 二分类问题中混淆矩阵、PR以及AP评估指标
  10. vue-cli-service &amp; @vue/cli-service