PAT甲级——1089 Insert or Merge (插入排序、归并排序的非递归实现)

本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/95494635

1089 Insert or Merge (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.

Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.

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 "Merge 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 0 6
1 3 2 8 5 7 4 9 0 6

Sample Output 2:

Merge Sort
1 2 3 8 4 5 7 9 0 6

题目大意:给出原始数组 init 以及排序过程中的数组 part ,判断是插入排序还是归并排序,并且输出下一次迭代后的数组。

思路:写个判断插入排序的函数,前面从小到大后面跟原始数组相同即为插排。归并排序是重点,这里需要模拟归并排序的非递归实现过程,每一趟归并前都将此时的数组与part比较,如果相同,则输出下一趟归并后的数组。

  1 #include <iostream>
  2 #include <vector>
  3 using namespace std;
  4 
  5 bool isInsertion(vector<int>& init, vector<int>& part, int& index);//判断是否为插入排序,index记录插排的最后执行位置
  6 bool isSame(vector<int>& A, vector<int>& B);
  7 void nextMerge(vector<int>& A, vector<int>& part);
  8 void mergePass(vector<int>& A, vector<int>& tmpA, int length);
  9 void merge(vector<int>& A, vector<int>& tmpA, int L, int R, int rightEnd);
 10 void nextInsert(vector<int>& A, int index);
 11 void print(vector<int>& A);
 12 
 13 int main()
 14 {
 15     int N, index = 0;
 16     scanf("%d", &N);
 17     vector <int> init(N), part(N);
 18     for (int i = 0; i < N; i++)
 19         scanf("%d", &init[i]);
 20     for (int i = 0; i < N; i++)
 21         scanf("%d", &part[i]);
 22     bool flag = isInsertion(init, part, index);
 23     if (flag) {
 24         printf("Insertion Sort\n");
 25         nextInsert(part, index);
 26     }
 27     else {//进行下一趟归并
 28         printf("Merge Sort\n");
 29         nextMerge(init, part);
 30     }
 31     return 0;
 32 }
 33 
 34 void nextMerge(vector<int>& A, vector<int>& part) {
 35     int length = 1,
 36         N = A.size();
 37     bool flag = false;
 38     vector<int> tmpA(N);
 39     while (length < N) {
 40         flag = isSame(part, A);
 41         mergePass(A, tmpA, length);
 42         if (flag) {
 43             print(tmpA);
 44             break;
 45         }
 46         length *= 2;
 47         flag = isSame(part, tmpA);
 48         mergePass(tmpA, A, length);
 49         if (flag) {
 50             print(A);
 51             break;
 52         }
 53         length *= 2;
 54     }
 55 }
 56 
 57 void mergePass(vector<int>& A, vector<int>& tmpA, int length) {
 58     int i,
 59         N = A.size();
 60     for (i = 0; i <= N - length * 2; i += length * 2)
 61         merge(A, tmpA, i, i + length, i + length * 2 - 1);
 62     if (i < N - length) //合并最后两个子列
 63         merge(A, tmpA, i, i + length, N - 1);
 64     else {//剩下最后一个子列,原样复制
 65         for (int j = i; j < N; j++) {
 66             tmpA[j] = A[j];
 67         }
 68     }
 69 }
 70 
 71 void merge(vector<int>& A, vector<int>& tmpA, int L, int R, int rightEnd) {
 72     int tmpL = L,
 73         leftEnd = R - 1;
 74     while (L <= leftEnd && R <= rightEnd) {
 75         if (A[L] < A[R])
 76             tmpA[tmpL++] = A[L++];
 77         else
 78             tmpA[tmpL++] = A[R++];
 79     }
 80     while (L <= leftEnd)
 81         tmpA[tmpL++] = A[L++];
 82     while (R <= rightEnd)
 83         tmpA[tmpL++] = A[R++];
 84 }
 85 
 86 bool isSame(vector<int>& A, vector<int>& B) {
 87     for (int i = 0; i < A.size(); i++)
 88         if (A[i] != B[i])
 89             return false;
 90     return true;
 91 }
 92 
 93 void nextInsert(vector<int>& A, int index) {
 94     int i,
 95         tmp = A[index];
 96     for (i = index; i > 0; i--) {
 97         if (A[i - 1] > tmp)
 98             A[i] = A[i - 1];
 99         else {
100             break;
101         }
102     }
103     A[i] = tmp;
104     print(A);
105 }
106 
107 bool isInsertion(vector<int>& init, vector<int>& part, int& index) {
108     int i;
109     for (i = 1; i < part.size(); i++) {
110         if (part[i] < part[i - 1]) {
111             index = i;
112             break;
113         }
114     }
115     for (; i < part.size(); i++) {
116         if (part[i] != init[i]) {
117             return false;//归并排序
118         }
119     }
120     return true;//插入排序
121 }
122 
123 void print(vector<int>& A) {
124     for (int i = 0; i < A.size(); i++) {
125         if (i != 0)
126             printf(" ");
127         printf("%d", A[i]);
128     }
129     printf("\n");
130 }

 

转载于:https://www.cnblogs.com/yinhao-ing/p/11171340.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值