Insert sort + non-recursive merge and sort 09-sort 2 Insert or Merge (25 points)

09-Sort 2 Insert or Merge (25 points)

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 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

Solve the problem
Write out insertion sorting and merge sorting, judge after each sorting, if it is in the same order as the given number sequence, it means the sorting method, and output after one-step sorting;

1. The input function
T is used for insertion sorting;
I is used for merge sorting;

#include<iostream>
using namespace std;
#define MAXN 101

int N;
int T[MAXN]; 
int I[MAXN];
int Result[MAXN];
void input()
{
	cin>>N;
	for(int i=0;i<N;i++)
	cin>>T[i];
	
	for(int i=0;i<N;i++) I[i]=T[i];
	
	for(int i=0;i<N;i++)
	cin>>Result[i];
}

2. Insert sorting
From subscripts 1 to N-1, compare with the previous numbers one by one. If the front number is large, the large number moves backward, and if the front number is small, it is inserted after it;

void InsertSort()
{
	int i;
	
	for(int i=1;i<N;i++)
	{
		int tmp=T[i];
		int j;
		for(j=i;j>0&&T[j-1]>tmp;j--)
			T[j]=T[j-1];
		T[j]=tmp;
		
		//判断是否与某一步骤吻合 
		int flag=1;
		
		for(int t=0;t<N;t++)
		{
			if(T[t]!=Result[t])
				{
					flag=0;
					break;
				 }	  
		}

		//执行输出 
		if(flag==1){
			cout<<"Insertion Sort"<<endl;
			tmp=T[i+1];
			for(j=i+1;j>0&&T[j-1]>tmp;j--)
				T[j]=T[j-1];
			T[j]=tmp;
			for(int t=0;t<N-1;t++)
			cout<<T[t]<<" ";
			cout<<T[N-1];
			return;
		}
	}
}

3. Merge sort (non-recursive)
merge:
put the L ~ R-1 and R ~ Rightend parts of A into TmpA, and then copy them back from TmpA;

void Merge( int A[], int TmpA[], int L,int R, int RightEnd)
 { //将有序的A[L]~A[R-1]和A[R]~A[RightEnd]归并成一个有序序列
 	int LeftEnd,tmp;
	int i;
	int Num;
	
	LeftEnd = R-1;
	tmp=L; //保存起始位置
	Num = RightEnd-L+1; //计算要归并的数的个数
	
	while(L <= LeftEnd && R<=RightEnd){ //两个都没有遍历完
		if(A[L]<=A[R])
			TmpA[tmp++]=A[L++];
		else
			TmpA[tmp++]=A[R++]; 
	} 
	
	while(L<=LeftEnd){   //若右边完了,左边还有,则左边添进去 
		TmpA[tmp++]=A[L++];
	}
	while(R<=RightEnd){
		TmpA[tmp++]=A[R++];
	}
	
	for( i =0;i<Num;i++,RightEnd--)
		A[RightEnd]=TmpA[RightEnd];       //把这些数全部拷贝到A中 
 }

Combine the number of each length in A into TmpA in pairs. When there is one pair left in the tail, perform a merge again. If there is only one tail, copy it directly back to A.

void Merge_pass(int A[], int TmpA[],int N,int length)
{ //两两归并相邻有序序列
	int i,j;
	
	for(i=0;i<=N-2*length;i+=2*length)  //处理成对的部分
		Merge(A,TmpA,i,i+length,i+2*length-1);
		
	if(i+length<N)
		Merge(A,TmpA,i,i+length,N-1);
	else 
		for(j=i;j<N;j++) TmpA[j]=A[j];       //把A赋值给TmpA	 
	
}

Complete merge, when the length is initialized to 1, the merge starts from a single number until all numbers are merged;
during the period, if the judgment function judges that the current sequence is the same as the given sequence, it can be judged to be merged and sorted this time, and output.

int Judge(int F[])
{
	int flag=1;
	for(int i=0;i<N;i++)
	{
		if(I[i]!=Result[i])
		{
			flag=0;
			break;
		}
	}
	if(flag==0) return 0;
	if (flag==1) return 1;
}

void MergeSort()
{
	int * TmpA=new int [N];
	int length =1;
	

		while(length<N)
		{
			Merge_pass(I,TmpA,N,length);
			length *=2;
			if(Judge(TmpA)) {
			cout<<"Merge Sort"<<endl;
			Merge_pass(TmpA,I,N,length);
			for(int i=0;i<N-1;i++)
			cout<<I[i]<<" ";
			cout<<I[N-1];
				return;
			}
//			Merge_pass(TmpA,I,N,length);
//			length*=2;
//			if(Judge(I)){
//			cout<<"Merge Sort"<<endl;
//			Merge_pass(I,TmpA,N,length);
//			for(int i=0;i<N-1;i++)
//			cout<<TmpA[i]<<" ";
//			cout<<TmpA[N-1];
//				return;
//			}
			
		}
		delete TmpA;

}

Because A and TmpA are the same every time the Merge function returns, so the loop only needs to judge once

4.main function

int main()
{
	input();
	InsertSort();
	MergeSort();
}

Note that the
recursive sorting method is difficult to get the results of each step, and the
non-recursive method can get the results of each step.

More convenient method

First determine whether it is an insertion sort: the
front is sorted from small to large, and if it is not satisfied, the back is the same as the original array order, it is the insertion sort;
otherwise it is the merge sort; the
difficulty is
how to get the next step of merge sort?

for ( l=2; l<=N; l*=2 )

Assuming the number of merges is 2, see if the 2nd and 3rd are satisfied; see if the 2 + 2i and 3 + 2i are satisfied;
if both are satisfied, then i is at least 4;
if there are not, then the current i is The number of merged, then merge every 2i number;

Published 105 original articles · won praise 6 · views 4946

Guess you like

Origin blog.csdn.net/BLUEsang/article/details/105555424