标签:
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 <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 #define MAX 110 6 char str[2][30]={ 7 "Insertion Sort", 8 "Merge Sort" 9 }; 10 int ori[MAX]; 11 int after[MAX]; 12 bool Judge(int A[],int B[] ,int N) 13 { 14 for(int i=0;i<N;i++) 15 { 16 if(A[i]!=B[i]) 17 return false; 18 } 19 return true; 20 } 21 void Print(int A[],int N) 22 { 23 for(int i=0;i<N;i++) 24 { 25 if(i==N-1) 26 printf("%d\n",A[i]); 27 else 28 printf("%d ",A[i]); 29 } 30 } 31 bool Insertion(int ori [],int after[] ,int N) 32 { 33 bool flag=false; 34 for(int i=1;i<N;i++) 35 { 36 int god=ori[i]; 37 int j; 38 for(j=i-1;j>=0;j--) 39 { 40 if(ori[j]>god) 41 { 42 ori[j+1]=ori[j]; 43 } 44 else 45 { 46 break; 47 } 48 } 49 ori[j+1]=god; 50 if(flag) 51 { 52 printf("%s\n",str[0]); 53 Print(ori,N); 54 break; 55 } 56 if(Judge(ori,after,N)) 57 { 58 flag=true; 59 } 60 } 61 return flag; 62 } 63 void Merge(int A[],int l1,int r1,int l2,int r2) 64 { 65 int temp[MAX]; 66 int i=l1,j=l2,index=0; 67 while(i<=r1&&j<=r2) 68 { 69 if(A[i]<A[j]) 70 { 71 temp[index++]=A[i]; 72 i++; 73 } 74 else 75 { 76 temp[index++]=A[j]; 77 j++; 78 } 79 } 80 while(i<=r1) 81 temp[index++]=A[i++]; 82 while(j<=r2) 83 temp[index++]=A[j++]; 84 for(int k=0;k<index;k++) 85 { 86 A[l1+k]=temp[k]; //因为都是相邻的两组进行合并 87 } 88 } 89 void Mergesort(int ori [],int after[] ,int N) 90 { 91 bool flag=false; 92 int step; 93 for(step=2;step/2<=N;step*=2) 94 { 95 for(int i=0;i<N;i+=step) 96 { 97 int mid=i+step/2-1; 98 if(mid+1>=N) 99 break; 100 Merge(ori,i,mid,mid+1,min(N-1,mid+step/2)); 101 } 102 if(flag) 103 { 104 printf("%s\n",str[1]); 105 Print(ori,N); 106 break; 107 } 108 if(Judge(ori,after,N)) 109 { 110 flag=true; 111 } 112 } 113 } 114 int main(int argc, char *argv[]) 115 { 116 int select;//选择是哪种排序 117 int N; 118 int temp[MAX]; 119 scanf("%d",&N); 120 for(int i=0;i<N;i++) 121 { 122 scanf("%d",&ori[i]); 123 temp[i]=ori[i]; 124 } 125 for(int i=0;i<N;i++) 126 { 127 scanf("%d",&after[i]); 128 } 129 if(Insertion(temp,after,N)) 130 return 0; 131 else 132 { 133 Mergesort(ori,after,N); 134 } 135 136 137 138 139 return 0; 140 }
标签:
原文地址:http://www.cnblogs.com/GoFly/p/4297485.html