A1089. 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<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 void merge(int num[], int low1, int high1, int low2, int high2){ 6 int temp[101]; 7 int index = 0, i = low1, j = low2; 8 while(i <= high1 && j <= high2){ 9 if(num[i] < num[j]) 10 temp[index++] = num[i++]; 11 else 12 temp[index++] = num[j++]; 13 } 14 while(i <= high1) 15 temp[index++] = num[i++]; 16 while(j <= high2) 17 temp[index++] = num[j++]; 18 for(int k = 0; k < index; k++) 19 num[low1 + k] = temp[k]; 20 } 21 int comp(int num1[], int num2[], int len){ 22 for(int i = 0; i < len; i++){ 23 if(num1[i] != num2[i]) 24 return 0; 25 } 26 return 1; 27 } 28 void mergeSort(int num[], int num2[], int len){ 29 int isSame = 1, show = 0; 30 for(int step = 2; step / 2 <= len; step *= 2){ 31 for(int i = 0; i < len; i += step){ 32 int mid = i + step / 2 - 1; 33 merge(num, i, mid, mid + 1, min(i + step - 1, len - 1)); 34 } 35 isSame = comp(num, num2, len); 36 if(isSame == 1){ 37 show = 1; 38 continue; 39 } 40 if(show == 1){ 41 printf("Merge Sort\n"); 42 printf("%d", num[0]); 43 for(int i = 1; i < len; i++) 44 printf(" %d", num[i]); 45 return; 46 } 47 } 48 } 49 void inser(int num[], int num2[], int len){ 50 int isSame = 1, show = 0; 51 for(int i = 1; i < len; i++){ 52 int temp = num[i]; 53 int j = i - 1; 54 while(j >= 0 && temp < num[j]){ 55 num[j + 1] = num[j]; 56 j--; 57 } 58 num[j + 1] = temp; 59 isSame = comp(num, num2, len); 60 if(isSame == 1){ 61 show = 1; 62 continue; 63 } 64 if(show == 1){ 65 printf("Insertion Sort\n"); 66 printf("%d", num[0]); 67 for(int i = 1; i < len; i++) 68 printf(" %d", num[i]); 69 return; 70 } 71 } 72 } 73 int main(){ 74 int N, num1[101], num1_[101], num2[101]; 75 scanf("%d", &N); 76 for(int i = 0; i < N; i++){ 77 scanf("%d", &num1[i]); 78 num1_[i] = num1[i]; 79 } 80 for(int i = 0; i < N; i++){ 81 scanf("%d", &num2[i]); 82 } 83 mergeSort(num1, num2, N); 84 inser(num1_, num2, N); 85 cin >> N; 86 return 0; 87 }
总结:
1、归并排序, 合并函数:
void merge(int num[], int low1, int high1, int low2, int high2){ int temp[101]; int index = 0, i = low1, j = low2; while(i <= high1 && j <= high2){ if(num[i] < num[j]) temp[index++] = num[i++]; else temp[index++] = num[j++]; } while(i <= high1) temp[index++] = num[i++]; while(j <= high2) temp[index++] = num[j++]; for(int k = 0; k < index; k++) num[low1 + k] = temp[k]; }
非递归写法:
void mergeSort(int num[], int len){
for(int step = 2; step <= len; step *= 2){ //初始步长为2, 逐步变为4、8、16 for(int i = 0; i < len; i += step){ //i + step为每个子区间的首部 int mid = i + step / 2 - 1; merge(num, i, mid, mid + 1, min(i + step - 1, len - 1)); } } }
2、插入排序:将a[0]至a[i]视作有序序列,将a[i + 1]插入a[0]至a[i]中,使之有序。
void inser(int num[], int len){
for(int i = 1; i < len; i++){ int temp = num[i]; int j = i - 1; while(j >= 0 && temp < num[j]){ num[j + 1] = num[j]; j--; } num[j + 1] = temp; } }
3、注意,排序用的num数组需要两个,归并排序之后num数组已经改变,不可再用于插入排序。