Pat Advanced 1089. Insert or Merge (25), 同Basic 1035. 插入与归并(25)
2015-06-30 22:07
417 查看
注意点:
这里的归并排序(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代码
【参考博客】
【方法一】:直接写出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
这里的归并排序(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
相关文章推荐
- 华为 2015 笔试题练习
- linux下 ls 排序
- @Autowired和@Resource装配
- html5+css3+javascript的幻想魅力(程序员的幻想)
- 程序员杂谈
- Linux 站在用户角度讲shell类型
- Functions
- Configurable API
- 自己写的两个JQ部件 其一 图片轮播SlideShow
- CSS使用总结
- Linux进程间通信-消息队列
- Nim教程【十四】
- PAT 1016 Phone bills
- Linux Centos
- struts的titles框架
- Maven环境搭建的步骤
- Camshift算法原理及其Opencv实现
- 自适应网站开发
- NSArray和NSMutableArray的相关总结
- 2015063002 - foreach不可以用于过滤元素