1098. Insertion or Heap Sort

原创 2015年11月17日 21:05:24

1098. Insertion or Heap Sort (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
#include<iostream>
#include<vector>
#include<stack>
#include<stdio.h>
using namespace std;

int min(int a, int b)
{
	return a > b ? b : a;
}

bool same(const vector<int> &v1, const vector<int> &v2)
{
	for(int i = 0; i < v1.size(); i ++)
	{
		if(v1[i] != v2[i])
			return false;
	}
	return true;
}

void maxHeapFixDown(vector<int> &original, int i, int n)
{
	int j, tmp = original[i];
	j = 2*i + 1;
	while(j < n)
	{
		if(j+1 < n && original[j+1] > original[j])
			j ++;
		if(original[j] <= tmp)
			break;
		original[i] = original[j];
		i = j;
		j = 2*i + 1;
	}
	original[i] = tmp;
}

void maxHeap(vector<int> &original)
{
	for(int i = original.size()/2 - 1; i >= 0; i --)
		maxHeapFixDown(original, i, original.size());
}

vector<vector<int> >v1;//insertSort
vector<vector<int> >v2;//heapSort 

void insertSort(vector<int> original)
{
	int length = original.size();
	for(int i = 1; i < length; i ++)
	{
		int j = i - 1;
		while(j >= 0 && original[j] > original[i])
			j --;
		int tmp = original[i];
		
		for(int k = i; k > j+1; k --)
			original[k] = original[k-1];
		original[j+1] = tmp;
		
		if(v1.size() == 0 || !same(v1[v1.size()-1], original))
			v1.push_back(original);
	}
}

void heapSort(vector<int> original)
{
	maxHeap(original);
	for(int i = original.size()-1; i >= 1; i --)
	{
		swap(original[i], original[0]);
		maxHeapFixDown(original, 0, i);
		v2.push_back(original);
	}
}

void print(const vector<int> &v)
{
	for(int i = 0; i < v.size(); i ++)
		if(i)
			cout<<" "<<v[i];
		else
			cout<<v[i];
	cout<<endl;
}

int main()
{
	freopen("F://Temp/input.txt", "r", stdin);
	int n;
	cin>>n;
	
	vector<int> input;
	vector<int> ans;
	
	for(int i = 0; i < n; i ++)
	{
		int num;
		cin>>num;
		input.push_back(num);
	}
	
	for(int i = 0; i < n; i ++)
	{
		int num;
		cin>>num;
		ans.push_back(num);
	}

	insertSort(input);
	for(int i = 0; i < v1.size(); i ++)
	{
		if(same(v1[i], ans))
		{
			cout<<"Insertion Sort"<<endl;
			if(i == v1.size()-1)
				print(v1[i]);
			else
				print(v1[i+1]);
				
			return 0;
		}
	}
	
	heapSort(input);
	for(int i = 0; i < v2.size(); i ++)
	{
		if(same(v2[i], ans))
		{
			cout<<"Heap Sort"<<endl;
			if(i == v2.size()-1)
				print(v2[i]);
			else
				print(v2[i+1]);
				
		
			return 0;
		}
	}

	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

1098. Insertion or Heap Sort (25)

According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and g...

Pat(Advanced Level)Practice--1098( Insertion or Heap Sort)

Pat1098代码 题目描述: According to Wikipedia: Insertion sort iterates, consuming one input elem...

1098. Insertion or Heap Sort

1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 ...

PAT 1098. Insertion or Heap Sort (25)

1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 ...

1098. Insertion or Heap Sort (25)

和插入和归并那道题差不多,新学交换元素技能,虽然没什么软用#include #include #include #include #pragma warning(disable:4996) using...

1098. Insertion or Heap Sort (25)解题报告

按通常的插入排序和堆排序。每排序一步,就比较一次。 对于宏,最好不要使用自增、自减运算,否则可能产生意想不到的错误。 #define _CRT_SECURE_NO_WARNINGS #in...

1098. Insertion or Heap Sort (25)

#include #include using namespace std; const int MAX = 110; int input[MAX],a[MAX],output[MAX]; int n...

1098. Insertion or Heap Sort (25)

题目如下: According to Wikipedia: Insertion sort iterates, consuming one input element each re...

PAT1098 Insertion or Heap Sort

题目链接: http://www.nowcoder.com/pat/5/problem/4322 题目大意:判断插入排序还是堆排序。首先判断下是哪种排序,是则以此排序方式再迭代一步。代码:#def...

1098. Insertion or Heap Sort (25)

1.之前一直开在测试点2里,通过实验知道0、2、4测试点是测插入排序的,1、3、5是堆排的。 2.测试点2卡住,主要是因为插入排序的外层循环我从i=0开始,导致第一步出来的结果和原来的一模一样(i=...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)