九、排序(上):Insert or Merge

根据给定的输入序列和部分排序结果,识别是使用插入排序还是归并排序,并进行下一步排序操作。通过分析部分排序序列的特点,确定排序方法并进行模拟,确保输出正确排序结果。
摘要由CSDN通过智能技术生成

题目描述

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

代码

#include<stdio.h>

#define MaxNum 100

void Insertion_Next_Iteration(int B[], int i, int N)
{
	int j, temp = B[i];
	for( j=i-1; j>=0; --j ){
		if( B[j]<=temp ){
			B[j+1] = temp;
			break;
		}	
		else B[j+1] = B[j];
	}
	if(B[0]>temp)
		B[0]=temp;
	for( j=0; j!=N-1; ++j )
		printf("%d ",B[j]);
    printf("%d",B[N-1]);
}

int Merge_Judge(int B[], int j, int length)
{/* 判断以j开始,长度为length的序列是否有序 */
	int i, k, S=1;
	if(length==1)
		return 1;
	for( i=j+1; i<=j+length-1; ++i )
		if( B[i]<B[i-1] ){
			S = 0;
			break;
		}
	return S;
}

void Merge(int A[], int *temp, int L, int R, int RightEnd)
{
	int LeftEnd = R - 1, tmp = L, Num = RightEnd - L + 1, i;
	while( L <= LeftEnd && R <= RightEnd ){
		if( A[L] <= A[R] ) temp[tmp++] = A[L++];
		else 			   temp[tmp++] = A[R++];
	}
	while( L <= LeftEnd )
		temp[tmp++] = A[L++];
	while( R <= RightEnd )
		temp[tmp++] = A[R++];
	/* for( i=0; i<Num; ++i,--RightEnd )
		A[RightEnd] = temp[RightEnd]; */
}

void Merge_Next_Iteration(int B[], int i, int N)
{
	int length = i - i%2;
	int j, k, S;
	int temp[MaxNum];
	
	while(length>=2){ /* 看当前归并算到哪一个length了 */
        S = 1;
		for( j=length; j<=N-1; j+=length ){
			if(j+length>N) {/* 末尾序列是不完整的 */
				if(!Merge_Judge(B,j,N-j)){/* 说明当前length下不满足有序性 */
					S=0;break;
				}
			}
			else 
				if(!Merge_Judge(B,j,length)){
					S=0;break;
				}
		}
		if(!S) /* 当前的length不满足有序性,应该继续除以2 */
			length = length/2;
		else 
			break;
	}
	
	/* 再进行一次归并 */
	for( j=0; j<=N-2*length; j+=2*length )
		Merge( B, temp, j, j+length, j+2*length-1 );
	if( j+length < N )
		Merge( B, temp, j, j+length, N-1 );
	else
		for( k=j; k!=N; ++k )
			temp[k] = B[k];
		
	for(j=0;j<N-1;++j)
		printf("%d ",temp[j]);
    printf("%d",temp[N-1]);
}

void Judge(int A[], int B[], int N)
{
	int i, j, name, S=1;
	for( i=1; i!=N; ++i )
		if( B[i]<B[i-1] ) /* 找到有序序列的末尾i-1 */
			break;
	for( j=i; j!=N; ++j )
		if( A[j]!=B[j] ){ /* 看后面的序列有没有进行过操作 */
			S = 0;
			break;
		}
	if( S ){ /* 后面的序列没动过,说明对前i-1个进行了插入排序 */
		printf("Insertion Sort\n");
		Insertion_Next_Iteration(B,i,N);
	}
	else{ /* 否则为归并排序 */
		printf("Merge Sort\n");
		Merge_Next_Iteration(B,i,N);
	}
	
}

int main()
{
	int i, N, A[MaxNum], B[MaxNum];
	scanf("%d\n",&N);
	for( i=0; i!=N; ++i )
		scanf("%d",&A[i]);
	for( i=0; i!=N; ++i )
		scanf("%d",&B[i]);
	Judge(A,B,N);
	return 0;
}

解题思路和遇到的问题

假设原序列是A[ ],经过排序过程的序列是B[ ]。
1.检查B[ ]开头多少个元素是有序的。
2.检查B[ ]剩下的无序序列是不是和A[ ]完全相同,也就是没有做排序。如果是,符合插入排序的特点。否则是归并排序。
3.插入排序再做一步排序比较简单。
对于归并排序,首先要找到现在的Length是多少。先以1得到的有序部分长度为初始Length,检查B[ ]剩下部分是否符合分段有序性。如果符合,说明这个Length是正确的。如果不符合,就把Length除以2再判断。
卡住Mg检查片段长度的错误算法,连续长度有3种测试点未通过。原因是Merge_Next_Iteration函数中的S=1放在了while(length>=2)外面。S用来判断当前的Length是否符合有序条件。第一个Length不符合,S会变成0,需要检查第二个Length,这时候需要重新初始化为1。所以S=1应该放在while(length>=2)里面。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值