09-排序3 Insertion or Heap Sort (25分)
2015-10-26 08:59
441 查看
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?
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.
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.
时间限制:400ms
内存限制:64MB
代码长度限制:16kB
判题程序:系统默认
作者:陈越
单位:浙江大学
http://pta.patest.cn/pta/test/18/exam/4/question/635
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 thatthe 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
时间限制:400ms
内存限制:64MB
代码长度限制:16kB
判题程序:系统默认
作者:陈越
单位:浙江大学
题目判定
http://pta.patest.cn/pta/test/18/exam/4/question/635// Insertion Sort Heap Sort #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <string> #include <vector> #include <algorithm> #include <sstream> using namespace std ; #define INF 999999 #define N 110 int n , m ; int a ; int b1 ; int b2 ; bool isOk1() { int i ; for(i = 0 ; i < n ; i ++) { if(a[i] != b1[i]) return false ; } return true ; } bool cmp(int x , int y) { return x < y ; } bool Insertion_Sort() { int i ; for(i = 2 ; i <= n ; i++) { sort(a , a+i ,cmp); if(isOk1()) { if(i < n) { sort(a , a+i+1 , cmp) ; } printf("Insertion Sort\n"); printf("%d" , a[0]) ; for(int j = 1 ; j < n ; j ++) { printf(" %d" , a[j] ); } printf("\n") ; return true ; } } return false ; } void adjust(int i , int m) { int tmpi = i ; while(tmpi < m) { int nowData = b2[tmpi] ; int left = -1 , right = -1;; if(tmpi*2 <= m) left = b2[tmpi*2] ; if(tmpi*2 + 1 <= m) right = b2[tmpi*2 + 1] ; if(b2[tmpi] > left && b2[tmpi] > right) break; if(left >= right) { if(left > b2[tmpi]) { b2[tmpi] = left ; b2[2*tmpi] = nowData ; tmpi = tmpi * 2 ; } }else{ if(right > b2[tmpi]) { b2[tmpi] = right ; b2[2*tmpi + 1] = nowData ; tmpi = tmpi * 2 + 1; } } } } void Heap_Sort(int m) { int i ; for(i = m / 2 ; i >= 1 ; i --) { adjust(i,m); } } int main() { //freopen("in.txt","r",stdin); int i ; while( scanf("%d" , &n) != EOF) { for(i = 0 ; i < n ; i++) { scanf("%d" , &a[i]) ; } for(i = 0 ; i < n ; i++) { scanf("%d" , &b1[i]) ; b2[i+1] = b1[i] ; } if( !Insertion_Sort()) { printf("Heap Sort\n"); int pos = n ; while(b2[pos--] > b2[1]) ; pos++ ; int tmp = b2[1]; b2[1] = b2[pos]; b2[pos] = tmp ; int m = pos - 1 ; Heap_Sort(m); printf("%d" , b2[1]) ; for(int j = 2 ; j <= n ; j ++) { printf(" %d" , b2[j] ); } printf("\n") ; } } return 0 ; }
相关文章推荐
- 英语需要你说出来
- FFmpeg的Android平台移植—编译篇
- 数学火星观(1)
- 几个 Ceph 性能优化的新方法和思路(2015 SH Ceph Day 参后感)
- 【c++】必须在类初始化列表中初始化的几种情况
- SharedPreferences的学习
- ios加载gif图片
- 自己动手写操作系统80x86保护模式(4)--分页机制
- [STAThread] 作用
- PHP 程序员学数据结构与算法之《栈》
- angular.bind使用心得
- 数据结构总览
- Phonegap VS AppCan 差异性对比
- Java解析XML格式数据的方法详解
- jQuery Ajax 实例 ($.ajax、$.post、$.get)
- View代码布局
- java中sleep()和wait()的区别
- 安卓解析中JSON中getString() 和optString()的区别
- 教训:任何的程序脚本,即便你认为再没有问题,也要测试一下再上线。
- 构造干净的 Git 历史线索