PAT 1098 Insertion or Heap Sort

来源:互联网 发布:java的分支语句课件 编辑:程序博客网 时间:2024/06/03 13:19

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 Sort

5 4 3 1 0 2 6 7 8 9

题目:

给定一个输入序列 再给一个插入排序或者堆排序的中间一趟结果,要求判断该中间序列是何种排序得出的,并输入用该种排序方法得出的下一趟排序结果

解法&&思路:

先判断是否是插入排序,判断方法,从末尾往前比较输入序列和中间排序序列的元素,记下第一个不相等元素的位置,此时,若该位置之前的所有元素都是有序的,则可以判定是插入排序的结果,否则是堆排序

输出下一趟的排序结果,关键是判定当前序列的状态,即排序进行到第几趟?哪些元素是已经排好序的

若是插入排序,上述第一个不相等元素位置即为插入排序已排序部分的尾部,取下一个元素插入即可。

若是堆排序,先给初始输入排好序,用它和中间序列比较(从后往前比),第一个不相等元素的位置即为堆尾,对前面元素组成的大根堆取顶调整即可

极端case:

测试点2可能会遇到麻烦,其原因在于,用上述方法找插入排序的现状态时,如果输入序列和中间序列相等,且前n个元素有序的情况下,无法判断到底是未经过排序还是已经排序过n次

根据测试下来看,题目认为这种情况下应该认为已经排序n次,下面应该插入n+1个元素

以后需注意:

给定一个插入排序的中间状态序列,可能不能唯一确定它排了一次

Code:

#include <iostream>#include <vector>#include <string>#include <stdio.h>#include <algorithm>void PrintVector(std::vector<int> half_sort) {printf("Vector is: ");for (int i = 0; i < half_sort.size() - 1; i++) {printf("%d ", half_sort[i]);}printf("%d\n", half_sort.back());}bool IsInsert(std::vector<int> init, std::vector<int>* half_sort, int size) {int mark = size-1;for (int i = size - 1; i > 0; i--) {if (init[i] == (*half_sort)[i]) {mark = i;}else {break;}}sort(init.begin(), init.begin()+mark);if (init == *half_sort) {int first_des = -1;for (int i = 0; i < half_sort->size()+1; i++) {if ((*half_sort)[i] >(*half_sort)[i+1]) {first_des = i;break;}}if (first_des != -1) {int temp = (*half_sort)[first_des+1];int pos = first_des;while (pos >= 0 && temp < (*half_sort)[pos]) {pos--;}for (int i = first_des + 1; i > pos+1 ;i--) {(*half_sort)[i] = (*half_sort)[i-1];}(*half_sort)[pos + 1] = temp;}return true;}return false;}void HeapAdjust(std::vector<int>* heap, int start, int end) {if (end > 2 * start) {int more_it = (end ==2*start+1)? end: (*heap)[2 * start + 1] > (*heap)[2 * start + 2] ? 2 * start + 1 : 2 * start + 2;if ((*heap)[start] < (*heap)[more_it]) {int temp = (*heap)[start];(*heap)[start] = (*heap)[more_it];(*heap)[more_it] = temp;}HeapAdjust(heap, more_it, end);}}void HeapSort(std::vector <int>* half_sort) {std::vector<int> temp_vector = (*half_sort);sort(temp_vector.begin(), temp_vector.end());int end = temp_vector.size()-1;while (end --) {if ((*half_sort)[end] != temp_vector[end]) {break;}}int temp = (*half_sort)[0];(*half_sort)[0] = (*half_sort)[end];(*half_sort)[end] = temp;// PrintVector(*half_sort);HeapAdjust(half_sort, 0, end-1);}int main() {int n;std::vector<int> init;std::vector<int> half_sort;std::cin >> n;for (int i = 0; i < n; i++) {int temp;std::cin >> temp;init.push_back(temp);}for (int i = 0; i < n; i++) {int temp;std::cin >> temp;half_sort.push_back(temp);}if (IsInsert(init, &half_sort, n)) {printf("Insertion Sort\n");} else {printf("Heap Sort\n");HeapSort(&half_sort);}for (int i = 0; i < half_sort.size() - 1; i++) {printf("%d ", half_sort[i]);}printf("%d", half_sort.back());system("pause");}

0 0
原创粉丝点击