PAT 1098 Insertion or Heap Sort

1098 Insertion or Heap Sort (25分)

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

 考點是插入排序和堆排序

這道題有點難理解誒,如果對堆排序插入排序不熟看答案也不好看出來

題意:給出長度爲 n 的兩個序列 a,b,a爲原始序列,b爲其排序的一個步驟結果,讓你看這排序是插入排序還是堆排序。並且輸出它的下一次排序序列

分析:

1. 插入排序的特點是:b數組前面的順序是有序的(min->max),後面的順序與原數組a是相同的,所以判斷方法是遍歷前面幾位。遇到順序不對的情況,就判斷此處數組b和數組的對應位置的值是不是相等,如果是就說明是插入排序,否則是堆排序。

2. 插入排序的下一步就是把第一個不符合從小到大的順序的那個元素插入到前面已經排好序的序列的合適位置,所以只要對前幾個已經排好序的後面一位的這個序列sort排序即可

3.堆排序的特點是:b數組後面是有序的(min->max),前面的順序不一定,那麼就可以從後面掃描,掃描到第一個數比前面那個數小的數字,記錄其位置ind,和第一個數字交換,然後把 1~ind-1進行一次向下調整,i和n是需要調整的區間,因爲是大頂堆,所以就是不斷比較當前結點和自己的孩子結點那個大,如果孩子結點大就交換,然後再不斷調整直到到達區間的最大值直到不能再繼續爲止

#include <bits/stdc++.h>
using namespace std;
int n;
int a[101], b[101];
void downJust(int i, int n) {
	int j = 2 * i;
	while(j <= n) {
		if(j + 1 <= n && b[j] < b[j + 1]) ++j;
		if(b[j] > b[i]) {
			swap(b[i], b[j]);
			i = j;
			j *= 2;
		} else break;
	}

}
int main() {
	memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b));
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i];
	for(int i = 1; i <= n; i++) cin >> b[i];
	int ind = 2, pos;
	while(ind <= n && b[ind - 1] <= b[ind]) ind++; //前面 min->max
	pos = ind;
	while(ind <= n && a[ind] == b[ind]) ind++; //後面 a[] = b[]
	//說明是插入排序
	if(ind == n + 1) {
		cout << "Insertion Sort" << endl;
		sort(b + 1, b + pos + 1);
	} else {
		cout << "Heap Sort" << endl;
		ind = n;
		while(ind > 1 && b[ind - 1] <= b[ind]) ind--;
		swap(b[1], b[ind]);
		downJust(1, ind - 1);
	}
	for(int i = 1; i <= n; i++) {
		cout << b[i];
		if(i != n) {
			cout << " ";
		} else cout << endl;
	}

	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章