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 resulting 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 <cstdlib>
#include <algorithm>
using namespace std;
void output(int arr[], int n);
bool judge(int a[], int b[], int n);
void heapify(int tree[], int n, int p);
void swap(int arr[], int i, int j);
void build_heap(int tree[], int n);
void heap_sort(int tree[], int n);
int main()
{
int ori[101], arr[101], srt[101], tmp[101], n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
ori[i] = arr[i];
}
for (int i = 0; i < n; i++)
cin >> srt[i];
int flag = 0;
for (int i = 1; i < n; i++)
{
if (i != 1 && judge(arr, srt, n))
flag = 1;
sort(arr, arr + i + 1);
if (flag)
break;
}
if (flag)
{
cout << "Insertion Sort" << endl;
output(arr, n);
}
else
{
int flag2 = 0;
build_heap(ori, n);
for (int i = n - 1; i >= 0; i--)
{
swap(ori, i, 0);
heapify(ori, i, 0);
if (flag2)
break;
if (judge(ori, srt, n))
flag2 = 1;
}
cout << "Heap Sort" << endl;
output(ori, n);
}
system("pause");
return 0;
}
void output(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
if (i > 0)
cout << " ";
cout << arr[i];
}
}
bool judge(int a[], int b[], int n)
{
for (int i = 0; i < n; i++)
{
if (a[i] != b[i])
return false;
}
return true;
}
void swap(int arr[], int i, int j)
{
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
void heapify(int tree[], int n, int p)
{
if (p >= n)
return;
int c1 = 2 * p + 1;
int c2 = 2 * p + 2;
int max = p;
if (c1 < n && tree[c1] > tree[max])
max = c1;
if (c2 < n && tree[c2] > tree[max])
max = c2;
if (max != p)
{
swap(tree, max, p);
heapify(tree, n, max);
}
}
void build_heap(int tree[], int n)
{
int last_node = n - 1;
int last_node_parent = (last_node - 1) / 2;
for (int i = last_node_parent; i >= 0; i--)
heapify(tree, n, i);
}