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
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<malloc.h> 4 #include<stdlib.h> 5 int A[110]; 6 int B[110]; 7 int L; 8 void Print(int N) 9 { 10 int i; 11 for (i = 0; i < N - 1; i++) 12 printf("%d ", B[i]); 13 printf("%d", B[i]); 14 } 15 int IsOrder(int B[],int lo, int hi) 16 { 17 for (int i = lo; i < hi - 1; i++) 18 if (B[i] > B[i + 1]) 19 return 0; 20 return 1; 21 } 22 int ChargeLength(int B[], int N, int Length) //检查以Length为长度的序列 23 { 24 int i; 25 for (i = 0; i < N -Length; i +=Length) 26 if (!IsOrder(B, i, i+Length)) 27 return 0; 28 if (i< N) 29 if (!IsOrder(B, i, N)) 30 return 0; 31 L = Length; 32 return 1; 33 } 34 int Charge(int B[], int N) 35 { 36 int Length = 2; 37 while(Length <=N) 38 { 39 if (ChargeLength(B, N, Length)) 40 Length *= 2; 41 else 42 if (Length > 2) 43 return 1; 44 else 45 return 0; 46 } 47 } 48 49 void Insert_Once(int B[],int i,int N) 50 { 51 int j; 52 int Temp = B[i]; 53 for (j = i; j > 0 && B[j - 1] >Temp; j--) 54 B[j] = B[j - 1]; 55 B[j] = Temp; 56 } 57 58 void Merge(int lo, int mi, int hi) 59 { 60 int* B1 = (int*)malloc(sizeof(int) * (mi - lo)); 61 for (int i = 0; i < mi - lo; i++)B1[i] = B[lo + i]; 62 int i, j, k; 63 i = lo; 64 j = 0; 65 k = mi; 66 while (j < mi - lo) 67 { 68 if (k >= hi || B1[j] <= B[k])B[i++] = B1[j++]; 69 if (k<hi && B1[j]>B[k])B[i++] = B[k++]; 70 } 71 free(B1); 72 } 73 void Merge_Once(int B[], int Length, int N) 74 { 75 int i; 76 for (i = 0; i < N - 2 * Length; i += 2 * Length) 77 Merge(i, i + Length, i + 2 * Length); 78 if (i + Length < N) 79 Merge(i, i + Length, N); 80 } 81 82 int main() 83 { 84 int N; 85 int Flag = 0; 86 int OrderPosition = 0; 87 scanf("%d", &N); 88 for (int i = 0; i < N; i++) 89 scanf("%d", &A[i]); 90 for (int i = 0; i < N; i++) 91 scanf("%d", &B[i]); 92 if (Flag=Charge(B, N)) 93 printf("Merge Sort\n"); 94 else 95 printf("Insertion Sort\n"); 96 for (int i = 0; i < N - 1; i++) 97 if (B[i] <=B[i + 1]) //这里必须是大于等于 98 OrderPosition = i + 1; 99 else 100 break; 101 if (Flag) 102 Merge_Once(B, L, N); 103 else 104 Insert_Once(B, OrderPosition+1, N); 105 Print(N); 106 return 0; 107 }