PAT甲级——A1098 Insertion or Heap Sort

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 (≤). 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


 1 //注意,这里的排序为排序迭代过程中完成的某一步而已,并不是最终结果
 2 //我们可以直接用sort来代替排序的部分,无论是合并排序还是插入排序
 3 #include <iostream>
 4 #include <vector>
 5 #include <algorithm>
 6 using namespace std;
 7 int N, a;
 8 vector<int>nums, ans, temp;
 9 void down(int index, int n)
10 {
11     int t = temp[index];
12     while (2 * index + 1 < n)
13     {
14         int child = 2 * index + 1;
15         if (child + 1 < n && temp[child] < temp[child + 1])
16             ++child;
17         if (temp[child] > t)
18         {
19             temp[index] = temp[child];
20             index = child;
21         }
22         else
23             break;
24     }
25     temp[index] = t;
26 }
27 int main()
28 {
29     cin >> N;
30     for (int i = 0; i < N; ++i)
31     {
32         cin >> a;
33         nums.push_back(a);
34     }
35     for (int i = 0; i < N; ++i)
36     {
37         cin >> a;
38         ans.push_back(a);
39     }
40     temp = nums;
41     bool f = false;
42     for (int i = 1; i < N; ++i)//进行插入排序
43     {
44         sort(temp.begin(), temp.begin() + i + 1);//第一趟排序应该是排序前两个数,第i趟排序分别排序前i+1个数
45         if (temp == ans)
46         {
47             f = true;
48             cout << "Insertion Sort" << endl;
49             sort(temp.begin(), temp.begin() + i + 2);//再一次迭代
50             break;
51         }
52     }
53     if (f == false)//那就是堆排序
54     {
55         cout << "Heap Sort" << endl;
56         temp = nums;
57         for (int i = N / 2; i >= 0; --i)//将前一半元素进行下滤
58             down(i, N);
59         for(int i=N-1;i>0;--i)//N-1趟排序,每次排序得到一个第N-i大的值放到相应位置
60         {
61             swap(temp[i], temp[0]);
62             down(0, i);
63             if (temp == ans)
64             {
65                 swap(temp[i-1], temp[0]);//进行下一次迭代
66                 down(0, i - 1);
67                 break;
68             }
69         }
70     }
71     for (int i = 0; i < N; ++i)
72         cout << temp[i] << (i < N - 1 ? " " : "");
73     return 0;
74 }
原文地址:https://www.cnblogs.com/zzw1024/p/11356687.html