09-排序3 Insertion or Heap Sort
2016-05-01 08:57
543 查看
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?
NNN
(≤100\le 100≤100).
Then in the next line, NNN
integers are given as the initial sequence. The last line contains the partially sorted sequence of the
NNN
numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.
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.
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 integerNNN
(≤100\le 100≤100).
Then in the next line, NNN
integers are given as the initial sequence. The last line contains the partially sorted sequence of the
NNN
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 guaranteedthat 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 <stdlib.h> #include <stdio.h> #define FULL 99999 /* 评测结果 时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户 2016-05-01 08:51 答案正确 25 09-排序3 gcc 2 1 569985011 测试点结果 测试点 结果 得分/满分 用时(ms) 内存(MB) 要点提示 测试点1 答案正确 7/7 2 1 sample Ins 中间步骤,有不需要交换的元素 测试点2 答案正确 6/6 1 1 sample Hp 一般情况 测试点3 答案正确 2/2 1 1 最小N, Ins 第一步没变 测试点4 答案正确 2/2 2 1 最小N, Hp第1步 测试点5 答案正确 4/4 1 1 最大N,Ins 测试点6 答案正确 4/4 1 1 最大N,Hp,后面x位没变化 */ /*先逐步执行插入排序并比较。 如果插入排序不是,那么进入堆排序。 首先把读入的数据分成两段(已排序的和未排序的),利用插入排序的结果找到断点 对前半段未排序的执行判断是不是一个有效的最大堆,如果不是提前跳出,否则,执行一次排序并输出*/ int Insertion(int *insert,const int*order,const int len);//插入排序 int HeapSort(int*,int *,const int len);//堆排序 int isit(int*a,const int*order,const int len); void print(int*a,int len); int main() { int n; scanf("%d",&n); int insert[101]; insert[0]=FULL; int order[101]; order[0]=FULL; int f; for(int i=0; i<n; i++) scanf("%d",&insert[i+1]);//为便于堆排序的比较,这里从1开始存储 for(int i=0; i<n; i++) scanf("%d",&order[i+1]); if(!Insertion(&insert[1],&order[1],n)) HeapSort(order,insert,n); return 0; } int HeapSort(int*heap,int *order,const int len) { // print(heap,len+1); // print(order,len+1); int BreakPoint=0; for(int i=len;i>0;i--){ if(heap[i]!=order[i]){ BreakPoint=i; // printf("BreakPoint[%d]",i); break; } } int flag=1; for(int i=1;i<=BreakPoint/2;i++){ if(heap[i]<heap[i*2]){ flag=0;break; } } if(!flag)return 0; int temp=heap[BreakPoint]; heap[BreakPoint]=heap[1]; // print(&heap[1],len); int son=1; for(int i=1;i*2<BreakPoint;){ son=i*2; if(i*2<BreakPoint-1) if(heap[i*2]<heap[i*2+1])son=i*2+1; if(heap[son]>temp){ heap[i]=heap[son]; i=son; }else break; } heap[son]=temp; printf("Heap Sort\n"); print(&heap[1],len); return 0; } int Insertion(int *insert,const int*order,const int len) { int flag=0; for(int i=0; i<len; i++) { if(insert[i]<insert[i-1]) { int temp=insert[i]; int j; for(j=i; j>0; j--) { if(insert[j-1]>temp)insert[j]=insert[j-1]; else break; } insert[j]=temp; if(flag)break; } flag=isit(insert,order,len); } if(flag) { printf("Insertion Sort\n"); print(insert,len); return 1; } return 0; } void print(int*a,int len) { for(int i=0; i<len; i++) { if(i)printf(" "); printf("%d",a[i]); } printf("\n"); } int isit(int*a,const int*order,const int len) { for(int i=0; i<len; i++) { if(order[i]!=a[i]) { // print(a,len); // printf("\n"); return 0; } } return 1; }
相关文章推荐
- 快速幂
- 我的第一个嵌入式linux驱动3_完善2
- Bsst Time to Buy and Sell Stock系列
- 数据结构之队列
- 20145232 韩文浩 《Java程序设计》第9周学习总结
- 深入理解Java虚拟机(三)-垃圾收集
- C经典 二级指针
- MFC 的 HTTP 请求处理
- Docker 容器互联方法
- oop——谁获得最高奖学金问题
- Java千百问_06数据结构(003)_什么是基本类型包装器
- 设计模式(十三)适配器模式
- jamcoin
- C语言 文件操作8--fputs()和fgets()
- Java中使用javadoc命令生成文档
- 《狗日的青春》
- 第 19 章 CSS 其他样式
- vSphere-02 What is vCenter Single Sign-On?
- thinkphp3.2.3连接sqlserver 2008 R2 数据库
- Android-ExifInterface如何正确存取Double类型坐标