Insert and merge (PAT)

1. Title Description:

According to Wikipedia:
Insertion sorting is an iterative algorithm, which obtains the input data one by one and produces the ordered output sequence step by step. In each iteration, the algorithm takes an element from the input sequence and inserts it into the correct position in the ordered sequence. Iterate until all elements are in order.

The merging and sorting operations are as follows: first, the original sequence is regarded as N ordered subsequences with only one element, and then two adjacent ordered subsequences are merged in each iteration until only one ordered sequence is left.

Now, given the original sequence and the intermediate sequence generated by a sorting algorithm, what sort algorithm is this algorithm?

2. Enter Description:

Input the positive integer n (< 100) in the first row, then the N integers of the original sequence and the intermediate sequence generated by a sorting algorithm in the last row. It is assumed that the target sequence of sorting is ascending. Numbers are separated by spaces.

3. Output Description:

First, output "Insertion Sort" for Insertion Sort, or "Merge Sort" for Merge Sort in the first row; then output the result sequence that iterates another round with the sorting algorithm in the second row. The questions ensure that the results of each group of tests are unique. Numbers are separated by spaces, and no extra spaces are allowed at the end of the line.

4. Input example:

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

5. Output example:

Insertion Sort
1 2 3 5 7 8 9 4 6 0

6. Solutions:

1. To do this, we need to know two sorting algorithms: insert sorting and merge sorting
(I don't know that these two sorts can be studied separately. Note that iterative method rather than recursive method should be used for this problem.);
>2. Then we only need to add judgment to each round of two sorts;
3. Judge whether the ordered sequence of each round produces the intermediate sequence given in the use case
4. Output the sequence of this round after the next round of sorting
(the last thing to notice is that there is an example about insertion sorting in the test case.)

10
3 1 2 8 7 5 9 4 6 0
1 2 3 5 7 8 9 4 6 0
//Correct output
Insertion Sort
1 2 3 4 5 7 8 9 6 0
//Error output
Insertion Sort
1 2 3 5 7 8 9 4 6 0

Error reason: when we find the intermediate sequence, we will output its sequence after the next round of sorting, but we find that the sequence after the next round of sorting has not changed, because in the next round of sorting and even the following rounds of processes, the part of the sequence we want to sort itself has already been sorted, as follows is the sorting process of this use case:

Initial: 3 (1) 2 8 7 5 9 4 6 0
 First round: 1 3 (2) 8 7 5 9 4 6 0
 Second round: 1 2 3 (8) 7 5 9 4 6 0
 Third round: 1 2 3 8 (7) 5 9 4 6 0
 Round 4: 1 2 3 7 8 (5) 9 4 6 0
 Round 5: 1 23 5 7 8 (9) 4 6 0 / / intermediate sequence
 Round 6: 1 23 5 7 8 9 (4) 6 0
 Round 7: 1 2 3 4 5 7 8 9 (6) 0 / / correct answer
....

7. source code:

#include<stdio.h>
#include<stdlib.h>		
int main()
{
	int i,j,k,m,n=0,N;
	int	*Init_list1,*Init_list2,*Mid_list,temp;
	scanf("%d",&N);//Sequence size
	Init_list1=(int*)malloc(N*(sizeof(int)));//Initial sequence 1 - insert sort
	Init_list2=(int*)malloc(N*(sizeof(int)));//Initial sequence 2-merge sort
	Mid_list=(int*)malloc(N*(sizeof(int)));//Intermediate sequence		
	for(i=0;i<N;i++)
		scanf("%d",&Init_list1[i]);//Enter initial sequence 1
	for(i=0;i<N;i++)
		scanf("%d",&Mid_list[i]);//Input intermediate sequence
	for(i=0;i<N;i++)
		Init_list2[i]=Init_list1[i];//Copy initial sequence 1 - > initial sequence 2

	
	for(i=1;i<N;i++)//Insertion sort
	{
		m=0;
		for(k=0;k<N;k++)//Ergodic sequence 1 and intermediate sequence
			if(Init_list1[k]!=Mid_list[k])
			{
				m=1;//Determine whether sequence 1 and intermediate sequence are equal
				break;
			}
		if(m==0)
			n=1;//Intermediate sequence exists
		//Single round insertion sort
		temp=Init_list1[i];
		j=i-1;
		while(temp<Init_list1[j])
		{
			Init_list1[j+1]=Init_list1[j];
			j--;
		}
		Init_list1[j+1]=temp;
		//Output part
		if(n==1&&j<i-1)//(m==0) confirm the existence of intermediate sequence
		{//(J < i-1) when the intermediate sequence is determined, the sequence does not rotate, that is, the sequence has a position exchange
			printf("Insertion Sort\n");
			for(k=0;k<N;k++)//Output sorted sequence 1
				printf("%d ",Init_list1[k]);
			break;
		}
	}
	
	int L_start=0,L_end=0;
	int R_start=0,R_end=0;
	int *sort_arr;	
	sort_arr=(int*)malloc(N*(sizeof(int)));
	for(i=1;i<N;i*=2)//Merge sort
	{
		n=0;
		for(k=0;k<N;k++)//Empathy
			if(Init_list2[k]!=Mid_list[k])
			{
				n=1;
				break;
			}
		//Single round merge and sort
		for(L_start=0;L_start<N-i;L_start=R_end)
		{
			L_end=L_start+i;
			R_start=L_start+i;
			R_end=R_start+i;
			if(R_end>N)
				R_end=N;
			k=0;
			while( L_start < L_end && R_start < R_end)
				if(Init_list2[L_start]<Init_list2[R_start])
					sort_arr[k++]=Init_list2[L_start++];
				else
					sort_arr[k++]=Init_list2[R_start++];
			while(L_start<L_end)
				sort_arr[k++]=Init_list2[L_start++];

			while(k>0)
				Init_list2[--R_start]=sort_arr[--k];
			
		}
		//Output part
		if(n==0)
		{
			printf("Merge Sort\n");
			for(k=0;k<N;k++)
				printf("%d ",Init_list2[k]);
			break;
		}	
	}
	return 0;
}
Published 25 original articles, won praise 4, visited 1238
Private letter follow

Posted on Tue, 04 Feb 2020 05:39:20 -0500 by spivey