Pat Advanced 1089. Insert or Merge (25), 同Basic 1035. 插入与归并(25)

10 篇文章 0 订阅
7 篇文章 0 订阅

注意点:

这里的归并排序(Merge Sort),需要按照题意要求去归并。

题意:

归并排序进行如下迭代操作:先将原始序列看成N个只包含1个元素的有序子序列,

                                    然后每次迭代归并两个相邻的有序子序列,

                                    直到最后只剩下1个有序的序列。

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.

思路:

        方法一:直观的解法,分别写出 Insert Sort 和Merge Sort,然后比较每一次迭代后的顺序,判断并输出;

        方法二:理解插入和归并排序,利用标准模版库中的 Sort 函数,有序地一轮一轮 Sort 数组,来模拟插入和归并每轮的输出。


方法一:Java代码

import java.util.Scanner;

public class Main{
	static int arrLen;
	static int[] unSorted4Insert = new int[101];
	static int[] unSorted4Merge = new int[101];
	static int[] temp = new int[101];
	static String lineToJudge;
	static boolean onceMoreFlag=false;
	static boolean isMerge =false;
	static boolean isInsert=false;

//	{3,1,2,8}
	public static void insertSort02(){
		for(int i=1;i<arrLen;i++){
			if(unSorted4Insert[i]<unSorted4Insert[i-1]){
				int temp =unSorted4Insert[i];
				int j=i-1;
				while(j>=0 && unSorted4Insert[j]>temp){
					  unSorted4Insert[j+1]=unSorted4Insert[j];
					  j--;
				}
				unSorted4Insert[j+1]=temp;
			}
			
			if(onceMoreFlag){
				break;
			}
			StringBuilder sb =new StringBuilder();
			for(int f=0;f<arrLen;f++){
				sb.append(unSorted4Insert[f]+" ");
			}
			if(sb.toString().trim().equals(lineToJudge)){
				isInsert=true;
				onceMoreFlag=true;
			}
		}
	}	
	
	public static void merge02(int start01,int end01,int start02,int end02){
		int start1 =start01;
		int end1   =end01;
		int start2 =start02;
		int end2   =end02;
		int i=start01;
		
		while(start1<=end1 && start2<=end2){
			if(unSorted4Merge[start1]<=unSorted4Merge[start2]){
				temp[i]=unSorted4Merge[start1];
				i++;
				start1++;
			}
			else{
				temp[i]=unSorted4Merge[start2];
				i++;
				start2++;
			}
		}
		while(start1<=end1){
			temp[i]=unSorted4Merge[start1];
			i++;
			start1++;
		}
		while(start2<=end2){
			temp[i]=unSorted4Merge[start2];
			i++;
			start2++;
		}
		
		for(int f=start01;f<=end02;f++){
			unSorted4Merge[f]=temp[f];
		}

	}
	
	
	public static void mergeSort02(int start,int end){
		for(int mergeStepLen=2   ;mergeStepLen/2<arrLen;mergeStepLen*=2){
			for(int i=0 ;i<arrLen ;i+=mergeStepLen){
				int start1=i;
				int end1  =i+mergeStepLen/2-1;//下标和长度相差1
				
				int start2=end1+1;
					if(start2>=arrLen){break;}
				int a=i+mergeStepLen-1;
				int b=arrLen-1;
				int end2  =a<b?a:b;
				merge02(start1,end1,start2,end2);
			}
			
			
			if(onceMoreFlag){
				break;
			}
			StringBuilder sb =new StringBuilder();
			for(int f=0;f<arrLen;f++){
				sb.append(unSorted4Merge[f]+" ");
			}
			if(sb.toString().trim().equals(lineToJudge)){
				isMerge=true;
				onceMoreFlag=true;
			}
			
		}
	}
	
	
	public static void main(String[] args) {
		Scanner sc =new Scanner(System.in);
		arrLen =sc.nextInt();
		for(int i =0;i<arrLen;i++){
			unSorted4Insert[i]=sc.nextInt();
			unSorted4Merge[i]=unSorted4Insert[i];
		}
		
		StringBuilder sb =new StringBuilder();
		for(int i =0;i<arrLen;i++){
			sb.append(sc.nextInt()+" ");
		}
		lineToJudge = sb.toString().trim();
		
		
		insertSort02();
		
		if(isInsert){
			System.out.println("Insertion Sort");
			
			sb.setLength(0);
			for(int f=0;f<arrLen;f++){
				sb.append(unSorted4Insert[f]+" ");
			}
			System.out.print(sb.toString().trim());
		}
		else{
			mergeSort02(0,arrLen-1);
			
			System.out.println("Merge Sort");
			
			sb.setLength(0);
			for(int f=0;f<arrLen;f++){
				sb.append(unSorted4Merge[f]+" ");
			}
			System.out.print(sb.toString().trim());
		}
		
	}
}


【参考博客】

         【方法一】:直接写出Insert Sort 和 Merge Sort

         http://www.cnblogs.com/GoFly/p/4297485.html


         【方法二】:利用标准模版库提供的Sort 函数,模拟一轮一轮的Insert Sort 和 Merge Sort

        http://blog.csdn.net/cstopcoder/article/details/42567287

        http://tech.bingfengsa.com/a/20141201/39766.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值