自顶向下归并排序(Merge Sort)
一、自顶向下的归并排序思路:

1、先把数组分为两个部分。
2、分别对这两个部分进行排序。
3、排序完之后,将这两个数组归并为一个有序的数组。
重复1-3步骤,直到数组的大小为1,则直接返回。
这个思路用递归函数来实现最方便,其中mid的计算公式:mid = lo + (hi-lo)/2,lo初始化为0,hi初始化为input.length - 1。
二、代码实现
package com.qiusongde; import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut; public class Merge { private static Comparable[] aux; public static void sort(Comparable[] input) {
int N = input.length;
aux = new Comparable[N];
sort(input, 0, N-1);
} private static void sort(Comparable[] input, int lo, int hi) { if(lo >= hi)//just one entry in array
return; int mid = lo + (hi-lo)/2;
sort(input, lo, mid);
sort(input, mid+1, hi);
merge(input, lo, mid, hi); } private static void merge(Comparable[] input, int lo, int mid, int hi) { //copy input[lo,hi] to aux[lo,hi]
for(int i = lo; i <= hi; i++) {
aux[i] = input[i];
} int i = lo;
int j = mid + 1;
for(int k = lo; k <= hi; k++) {
if(i > mid)
input[k] = aux[j++];
else if(j > hi)
input[k] = aux[i++];
else if(less(aux[j], aux[i]))
input[k] = aux[j++];
else
input[k] = aux[i++];
} StdOut.printf("merge(input, %4d, %4d, %4d)", lo, mid, hi);
show(input);//for test } private static boolean less(Comparable v, Comparable w) { return v.compareTo(w) < 0; } private static void show(Comparable[] a) { //print the array, on a single line.
for(int i = 0; i < a.length; i++) {
StdOut.print(a[i] + " ");
}
StdOut.println(); } public static boolean isSorted(Comparable[] a) { for(int i = 1; i < a.length; i++) {
if(less(a[i], a[i-1]))
return false;
} return true; } public static void main(String[] args) { //Read strings from standard input, sort them, and print.
String[] input = In.readStrings();
show(input);
sort(input);
assert isSorted(input);
show(input); } }
测试数据:M E R G E S O R T E X A M P L E
输出结果:
M E R G E S O R T E X A M P L E
merge(input, 0, 0, 1)E M R G E S O R T E X A M P L E
merge(input, 2, 2, 3)E M G R E S O R T E X A M P L E
merge(input, 0, 1, 3)E G M R E S O R T E X A M P L E
merge(input, 4, 4, 5)E G M R E S O R T E X A M P L E
merge(input, 6, 6, 7)E G M R E S O R T E X A M P L E
merge(input, 4, 5, 7)E G M R E O R S T E X A M P L E
merge(input, 0, 3, 7)E E G M O R R S T E X A M P L E
merge(input, 8, 8, 9)E E G M O R R S E T X A M P L E
merge(input, 10, 10, 11)E E G M O R R S E T A X M P L E
merge(input, 8, 9, 11)E E G M O R R S A E T X M P L E
merge(input, 12, 12, 13)E E G M O R R S A E T X M P L E
merge(input, 14, 14, 15)E E G M O R R S A E T X M P E L
merge(input, 12, 13, 15)E E G M O R R S A E T X E L M P
merge(input, 8, 11, 15)E E G M O R R S A E E L M P T X
merge(input, 0, 7, 15)A E E E E G L M M O P R R S T X
A E E E E G L M M O P R R S T X
三、性能分析
结论1:对于长度为N的任意数组,自顶向下归并排序需要1/2NlgN至NlgN次比较(less(aux[j], aux[i]))。
分析:见P272
结论2:对于长度为N的任意数组,自顶向下归并排序所需要的数组访问最大次数为6NlgN。
分析:每调用merge函数一次,2N次数组访问用于复制,2N次用于将排好序的元素移动回去,还有最多2N次用于比较。
四、算法改进
1、切换为插入排序
对于小数组来说,快速排序比插入排序慢。
2、测试数组是否已经有序
添加一个判断条件,如果a[mid]小于等于a[mid+1],我们就认为数组是有序的了,并跳过merge函数。
private static void sort(Comparable[] input, int lo, int hi) {
if(lo >= hi)//just one entry in array
return;
int mid = lo + (hi-lo)/2;
sort(input, lo, mid);
sort(input, mid+1, hi);
if(!less(input[mid+1],input[mid]))//input[mid+1] >= input[mid]
return;
merge(input, lo, mid, hi);
}
3、不将元素复制到辅助数组
这种方法需要在递归调用的每个层次交换输入数组和辅助数组的角色。
public static void sort(Comparable[] input) {
int N = input.length;
aux = input.clone();//must be clone(the same as input)
// StdOut.println("input:" + input + " aux:" + aux);//for test
sort(aux, input, 0, N-1);
}
//this level takes source as input(need to be sorted)
//and destination as auxiliary, and put the result in destination
private static void sort(Comparable[] source, Comparable[] destination, int lo, int hi) {//avoid copy
if(lo >= hi)//just one entry in array
return;
int mid = lo + (hi-lo)/2;
sort(destination, source, lo, mid);//down level switch the roles of the input array and auxiliary array
sort(destination, source, mid+1, hi);
merge(source, destination, lo, mid, hi);//merge sorted source to destination
}
private static void merge(Comparable[] source, Comparable[] destination, int lo, int mid, int hi) {
int i = lo;
int j = mid + 1;
for(int k = lo; k <= hi; k++) {
if(i > mid)
destination[k] = source[j++];
else if(j > hi)
destination[k] = source[i++];
else if(less(source[j], source[i]))
destination[k] = source[j++];
else
destination[k] = source[i++];
}
// StdOut.println("source:" + source + " destination:" + destination);//for test
// StdOut.printf("merge(source, destination, %4d, %4d, %4d)", lo, mid, hi);//for test
// show(destination);//for test
}
自顶向下归并排序(Merge Sort)的更多相关文章
- 经典排序算法 - 归并排序Merge sort
经典排序算法 - 归并排序Merge sort 原理,把原始数组分成若干子数组,对每个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到所有合并完,形成有序的数组 举例 无序数组[6 2 ...
- 连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort)
连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort) 1,起泡排序(bubble sort),大致有三种算法 基本版,全扫描. 提前终止版,如果发现前区里没有发生交换 ...
- 排序算法二:归并排序(Merge sort)
归并排序(Merge sort)用到了分治思想,即分-治-合三步,算法平均时间复杂度是O(nlgn). (一)算法实现 private void merge_sort(int[] array, int ...
- 归并排序(merge sort)
M erge sort is based on the divide-and-conquer paradigm. Its worst-case running time has a lower ord ...
- 归并排序Merge Sort
//C语言实现 void mergeSort(int array[],int first, int last) { if (first < last)//拆分数列中元素只剩下两个的时候,不再拆分 ...
- 归并排序——Merge Sort
基本思想:参考 归并排序是建立在归并操作上的一种有效的排序算法.该算法是采用分治法的一个非常典型的应用.首先考虑下如何将2个有序数列合并.这个非常简单,只要从比较2个数列的第一个数,谁小就先取谁,取了 ...
- 归并排序Merge sort(转)
原理,把原始数组分成若干子数组,对每一个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到全部合并完,形成有序的数组 举例 无序数组[6 2 4 1 5 9] 先看一下每个步骤下的状态, ...
- 数据结构 - 归并排序(merging sort)
归并排序(merging sort): 包含2-路归并排序, 把数组拆分成两段, 使用递归, 将两个有序表合成一个新的有序表. 归并排序(merge sort)的时间复杂度是O(nlogn), 实际效 ...
- 数据结构 - 归并排序(merging sort) 具体解释 及 代码
归并排序(merging sort) 具体解释 及 代码 本文地址: http://blog.csdn.net/caroline_wendy 归并排序(merging sort): 包括2-路归并排序 ...
随机推荐
- debian SSD ext4 4K 对齐
新入手了一台thinkpad, 原来的机械硬盘是500G的, 于是购入一块镁光的MX200 250G的SSD来新装debian stable(jessie) 1, 安装系统的之前按住F1进入bios后 ...
- jar包解压与打包
首先感谢大神的指导:https://blog.csdn.net/mr_pang/article/details/47028921 1.首先准备一个能运行的jar文件,我们使用第三方解压工具进行解压wi ...
- vue面试题,知识点汇总(有答案)
一. Vue核心小知识点 1.vue中 key 值的作用 key 的特殊属性主要用在 Vue的虚拟DOM算法,在新旧nodes对比时辨识VNodes.如果不使用key,Vue会使用一种最大限度减少动态 ...
- WPF编程学习——样式(好文)
http://www.cnblogs.com/libaoheng/archive/2011/11/20/2255963.html
- 安装android Studio和运行react native项目(基础篇)
ANDROID_HOME环境变量 确保ANDROID_HOME环境变量正确地指向了你安装的Android SDK的路径. 打开控制面板 -> 系统和安全 -> 系统 -> 高级系统设 ...
- Hadoop学习笔记——Hadoop经常使用命令
Hadoop下有一些经常使用的命令,通过这些命令能够非常方便操作Hadoop上的文件. 1.查看指定文件夹下的内容 语法: hadoop fs -ls 文件文件夹 2.打开某个已存在的文件 语法: h ...
- JSP具体篇——application
application对象 application对象用于保存全部应用程序中的共同拥有数据.它在server启动时自己主动创建.在server停止时自己主动销毁. 当application对象没有被销 ...
- TP框架---thinkphp中ajax分页
//点击类别后要显示的内容 public function pagechuli3()//这个方法的功能是根据ajax传过来的值查询数据,再将查询出来的数据返回到ajax,返回的默认是JSON类型. { ...
- 【BZOJ4070】[Apio2015]雅加达的摩天楼 set+最短路
[BZOJ4070][Apio2015]雅加达的摩天楼 Description 印尼首都雅加达市有 N 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 0 到 N−1.除了这 N 座摩天楼 ...
- Spring标签@Aspect-实现面向方向编程(@Aspect的多数据源自动加载)——SKY
从Spring 2.0开始,可以使用基于schema及@AspectJ的方式来实现AOP.由于@Aspect是基于注解的,因此要求支持注解的5.0版本以上的JDK. 环境要求: 1. mybit ...