本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941
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?
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.
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.
10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0
Insertion Sort
1 2 3 5 7 8 9 4 6 0
10
3 1 2 8 7 5 9 4 6 0
6 4 5 1 0 3 2 7 8 9
Heap Sort
5 4 3 1 0 2 6 7 8 9
题目大意: 对N个数据进行排序,给出排序过程中的数据,判断是插入排序还是堆排序,然后输出下一步排序得到的数列。
思路:判断排序方法,前面是从小到大排列,后面的数据跟原始数据相同的就是插入排序,否则为堆排序。
插入排序的下一步操作就是把下一个数字插入到前面合适的位置。
堆排序的首个数字是当前大顶堆的根,将它与堆的最后一个节点(从后往前找,第一个小于根节点的数就是当前堆的最后一个节点)交换位置,然后再对根节点进行堆化操作。
测试点0、2、4是插入排序,1、3、5是堆排序。(不要问我是怎么知道的~)
1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 int N; 5 vector <int> v1, v2; 6 int maxIndex(int a, int b, int c, int n); 7 bool fun(); 8 void adjust(int index, int n); 9 void swap(int& a, int& b); 10 int main() 11 { 12 scanf("%d", &N); 13 v1.resize(N); 14 v2.resize(N); 15 for (int i = 0; i < N; i++) 16 scanf("%d", &v1[i]); 17 for (int i = 0; i < N; i++) 18 scanf("%d", &v2[i]); 19 bool flag = fun();//判断排序方式 20 if (flag) {//插入排序 21 int index, tmp, j; 22 for (int i = 1; i < N; i++) 23 if (v2[i] < v2[i - 1]) { 24 index = i; 25 tmp = v2[index]; 26 break; 27 } 28 for (j = index; j > 0 && v2[j - 1] > tmp; j--) 29 v2[j] = v2[j - 1]; 30 v2[j] = tmp; 31 printf("Insertion Sort\n"); 32 } 33 else {//堆排序 34 int index, tmp; 35 for (int i = N - 1; i >= 0; i--) 36 if (v2[0] > v2[i]) { 37 index = i; 38 tmp = v2[index]; 39 break; 40 } 41 v2[index] = v2[0]; 42 v2[0] = tmp; 43 adjust(0, index - 1); 44 printf("Heap Sort\n"); 45 } 46 for (int i = 0; i < N; i++) { 47 printf("%d", v2[i]); 48 if (i < N - 1) 49 printf(" "); 50 } 51 return 0; 52 } 53 void adjust(int index, int n) { 54 int max; 55 while (index * 2 + 1 <= n ) { 56 max = maxIndex(index, index * 2 + 1, index * 2 + 2, n);//寻找当前节点与其孩子之间的最大值的下标 57 if (index == max)//若最大值是自己,则退出循环 58 break; 59 swap(v2[index], v2[max]); 60 index = max; 61 } 62 } 63 int maxIndex(int a, int b, int c, int n) { 64 if (v2[b] > v2[a]) 65 a = b; 66 if (c <= n && v2[c] > v2[a]) 67 a = c; 68 return a; 69 } 70 void swap(int& a, int& b) { 71 int c = a; 72 a = b; 73 b = c; 74 } 75 bool fun() { 76 int i, index = 0; 77 for (i = 1; i < N; i++) 78 if (v2[i] < v2[i - 1]) { 79 index = i; 80 break; 81 } 82 for (int i = index; i < N; i++) { 83 if (v1[i] != v2[i]) 84 return false; 85 } 86 return true; 87 }
PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)
原文:https://www.cnblogs.com/yinhao-ing/p/10981969.html