pat 1098 Insertion or Heap Sort
2015-09-30 19:59
330 查看
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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
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
#include <iostream> #include <string> #include <vector> using namespace std; bool myEquals(vector<int> a, vector<int> b) { for (int i = 0; i < a.size(); ++i) { if(a[i] != b[i]) return false; } return true; } void printVec(vector<int> vec) { for (int i = 0; i < vec.size(); ++i) { cout << vec[i]; if(i != vec.size()-1) { cout << " "; } } cout << endl; } bool insertSort(vector<int> insert, vector<int> target) { int len = insert.size(); bool flag = false; for (int i = 1; i < len; ++i) { int nTemp = insert[i]; bool tmp = false; for (int j = i-1; j >= 0; --j) { if(nTemp < insert[j]) insert[j+1] = insert[j]; else { insert[j+1] = nTemp; tmp = true; break; } } if(!tmp) { insert[0] = nTemp; } if(flag) { printVec(insert); break; } if(myEquals(target, insert)) { flag = true; } if(flag) { cout << "Insertion Sort" << endl; } } return flag; } void heapAdjust(vector<int>& heap, int i, int len) { int nChild; for (; i*2+1 < len; i = nChild) { nChild = i*2+1; if((nChild<len-1) && (heap[nChild] < heap[nChild+1])) nChild++; if(heap[i] < heap[nChild]) { int nTemp = heap[nChild]; heap[nChild] = heap[i]; heap[i] = nTemp; } else { break; } } } bool heapSort(vector<int> heap, vector<int> target) { int len = heap.size(); bool flag = false; for (int i = len/2-1; i >= 0; --i) { heapAdjust(heap, i, len); } for (int i = len-1; i >= 0; --i) { heap[i] = heap[0] ^ heap[i]; heap[0] = heap[0] ^ heap[i]; heap[i] = heap[i] ^ heap[0]; heapAdjust(heap, 0, i); if(flag) { printVec(heap); break; } if(myEquals(heap, target)) { flag = true; } if(flag) { cout << "Heap Sort" << endl; } } return flag; } int main() { int n, nTemp; vector<int> inital, target; cin >> n; for (int i = 0; i < n; ++i) { cin >> nTemp; inital.push_back(nTemp); } for (int i = 0; i < n; ++i) { cin >> nTemp; target.push_back(nTemp); } if(insertSort(inital, target)) { } else if(heapSort(inital, target)){ } return 0; }
相关文章推荐
- 抓包流程与配置
- 黑马程序员——语言基础——循环语句
- BZOJ 2300 [HAOI2011]防线修建 Splay维护动态凸包
- LintCode -- 二叉树的前序遍历
- MeteoInfoLab脚本示例:Trajectory
- Linux上的日志系统
- xUnit随笔
- 个人博客作业2
- 自己写一个程序 快速的删除文件
- Nosql数据库——redis(六)事务和持久化
- 歪果仁博客和Twitter账户
- Android 总结:打造Android中的流式布局和热门标签(源码有详细注释)
- Stored Properties 与 Computed Properties
- Android之旅(一):搭建Android开发环境
- 剑指offer—矩形覆盖
- opengl 版本
- 9月英语小结
- 20150920 加密和解密原理 Open SSL OPEN SSH使用
- 基于ActiveMQ的消息中间件系统 OneMM逻辑与物理架构设计详解
- UI_UITableView