PAT 1089.Insert or Merge
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 (<=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 "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 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 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
题目解析:
输入两个数组,解析第二个数组是第一个数组插入排序还是归并排序的中间过程。输出排序类型,别输出再执行一趟该排序后的结果。
该题目时间充裕,可直接先进行插入排序,每一趟检查是否与数组二相等。若都不符在开始执行归并排序。
1 #include<stdio.h> 2 #include<vector> 3 4 using namespace std; 5 6 int n; 7 vector<int> input, output, insert, merge; 8 9 bool com(vector<int> in){ 10 int i; 11 for (i = 0; i < n; i++){ 12 if (in[i] != output[i]){ 13 return false; 14 } 15 } 16 return true; 17 } 18 19 void Insert(int input){ 20 int i, j; 21 for (i = 0; i < input; i++){ 22 if (insert[i]>insert[input]){ 23 int temp = insert[input]; 24 for (j = input; j > i; j--){ 25 insert[j] = insert[j - 1]; 26 } 27 insert[i] = temp; 28 } 29 } 30 } 31 32 void doMerge(int start, int end){ 33 int i, j; 34 for (i = start + 1; i < end; i++){ 35 for (j = i; j >= start + 1; j--){ 36 if (merge[j] < merge[j - 1]){ 37 swap(merge[j], merge[j - 1]); 38 } 39 else{ 40 break; 41 } 42 } 43 } 44 } 45 46 void Merge(){ 47 int i, j; 48 bool flag = false; 49 for (i = 1; i < n; i *= 2){ 50 for (j = 0; j < n; j += i * 2){ 51 doMerge(j, j + i * 2); 52 } 53 if (flag){ 54 return; 55 } 56 if (com(merge)){ 57 flag = true; 58 } 59 } 60 } 61 62 int main(void){ 63 int temp; 64 scanf("%d", &n); 65 int i, j; 66 for (i = 0; i < n; i++){ 67 scanf("%d", &temp); 68 input.push_back(temp); 69 } 70 for (i = 0; i < n; i++){ 71 scanf("%d", &temp); 72 output.push_back(temp); 73 } 74 insert = input; 75 merge = input; 76 for (i = 1; i < n; i++){ 77 Insert(i); 78 if (com(insert)){ 79 Insert(i + 1); 80 printf("Insertion Sort\n"); 81 printf("%d", insert[0]); 82 for (i = 1; i < n; i++){ 83 printf(" %d", insert[i]); 84 } 85 printf("\n"); 86 return 0; 87 } 88 } 89 for (i = 1; i < n; i *= 2){ 90 for (j = 0; j < n; j += i * 2){ 91 if (j + i * 2 <= n) 92 doMerge(j, j + i * 2); 93 else 94 doMerge(j, n); 95 } 96 if (com(merge)){ 97 i *= 2; 98 for (j = 0; j < n; j += i * 2){ 99 if (j + i * 2 <= n) 100 doMerge(j, j + i * 2); 101 else 102 doMerge(j, n); 103 } 104 printf("Merge Sort\n"); 105 printf("%d", merge[0]); 106 for (int k = 1; k < n; k++){ 107 printf(" %d", merge[k]); 108 } 109 printf("\n"); 110 return 0; 111 } 112 } 113 return 0; 114 }