Title Description
According to the definition of Wikipedia:
Insertion sorting is an iterative algorithm, which obtains the input data one by one and gradually produces an orderly output sequence. In each iteration, the algorithm takes an element from the input sequence and inserts it into the correct position in the ordered sequence. This iterates until all elements are in order.
Merge sort performs the following iterative operations: first, the original sequence is regarded as N ordered subsequences containing only one element, and then two adjacent ordered subsequences are merged each iteration until only one ordered sequence is left.
Given the original sequence and the intermediate sequence generated by a sorting algorithm, please judge which sorting algorithm this algorithm is?
Input format:
Enter the positive integer N (≤ 100) given on the first line; The next line gives N integers of the original sequence; The last line gives the intermediate sequence generated by a sorting algorithm. Here, it is assumed that the target sequence of sorting is ascending. Numbers are separated by spaces.
Output format:
First, in line 1, output Insertion Sort to indicate Insertion Sort or Merge Sort to indicate Merge Sort; Then output the result sequence of another round of iteration with the sorting algorithm in line 2. The topic ensures that the results of each group of tests are unique. Numbers shall be separated by spaces, and there shall be no extra spaces at the beginning and end of the line.
Input example 1:
10 3 1 2 8 7 5 9 4 6 0 1 2 3 7 8 5 9 4 6 0
No blank lines at the end
Output example 1:
Insertion Sort 1 2 3 5 7 8 9 4 6 0
No blank lines at the end
Input example 2:
10 3 1 2 8 7 5 9 4 0 6 1 3 2 8 5 7 4 9 0 6
No blank lines at the end
Output example 2:
Merge Sort 1 2 3 8 4 5 7 9 0 6
No blank lines at the end
Test point 5: when merging and sorting the last group, even if the last group is less than 2*i, it shall be output after sorting
Test point 6: there will be a situation where the last group does not meet 2*i, so the program thinks it is only arranged to i, which is also wrong
For example:
10
3 1 2 8 7 5 9 4 0 6
1 2 3 8 4 5 7 9 6 0
Output should be
Merge Sort
1 2 3 4 5 7 8 9 0 6
4 per group instead of 2 per group
code:
#include<stdio.h> int i, j;//Global variable attention int f(int* b, int N); int panduan(int* a, const int* b, int N); int main() { int N, tmp , t,s; scanf("%d",&N); int a[N+5]; int b[N+5]; for( i = 0; i < N; i++) { scanf("%d",&a[i]); } for( i = 0; i < N; i++) { scanf("%d",&b[i]); } //Determine whether to insert sort or merge sort if( panduan(a,b,N) ) { //insert i++; tmp = a[i]; for( j = i-1; j>=0 && a[j] > tmp; j-- ) { a[j+1] = a[j]; } a[j+1] = tmp; printf("Insertion Sort\n"); for( j = 0; j < N; j++) { if(j != 0) { printf(" "); } printf("%d",a[j]); } } //Merge else { //Find merge sort several times s = f(b,N) /2; //printf("%d",s); //2*(i+1) //initialization int n = 0, m = 0; for(i = 0; i < N; i++) { a[i] = b[i]; } //sort for( i = 0; i< N; i += (2*s)) { n = 0, m=0; for(j = i; j < i+(2*s) && j < N; j++ ) { // Row all groups meeting 2*i if(N-j>s) { //a[j+n],a[i+s+m] if(n >= s) { a[j] = b[i+s+m]; m++; } else if(m >= s) { a[j] = b[i+n]; n++; } else { if(b[i+n] > b[i+s+m]) { a[j] = b[i+s+m]; m++; } else { a[j] = b[i+n]; n++; } } } } } //Row groups that do not meet 2*i for( i = i-(2*s) ; i < N-1 ; i++) { for(j = i; j < N-1-i; j++) { if(a[j] >a[j+1]) { tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } } } //output printf("Merge Sort\n"); for( i = 0; i < N; i++) { if(i != 0) { printf(" "); } printf("%d",a[i]); } } } int panduan(int* a, const int* b, int N) { int tmp, count; for(i = 1; i < N; i++) { count = 0; tmp = a[i]; for( j = i-1; j>=0 && a[j] > tmp; j-- ) { a[j+1] = a[j]; } a[j+1] = tmp; for( j = 0; j < N; j++) { if(a[j] == b[j]) { count++; } } if(count == N) { return 1; } } return 0; } int f(int* b, int N) { int t; for(i = 2; i < N; i *= 2) { for( t = 0; t+i <= N; t += i ) { for(j = t; j < t+i-1; j++) { if(b[j] > b[j+1]) { return i; } } } } }