自顶向下归并排序(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-路归并排序 ...
随机推荐
- MQTT服务器搭建--Apollo
尊重原创,我是伸手党:https://blog.csdn.net/u012377333/article/details/68943416 1.Apollo下载 下载地址:http://activemq ...
- Word 操作
1.出文件,最后一页是附件.最后一页的页码不想要.如何删除?用的是 office word 2010版本,跟07 03版本界面不一样. 在最后一页的最前面插入分节符:下一页 ,编辑页脚.让页脚“取消链 ...
- Android程序的打包和安装
当我们使用Android Studio的时候,这些步骤都交给它去做了. 编译 classes.dex 文件 编译 resources.arsc 文件 生成资源索引表resources.arsc. 把r ...
- Tomcat9源码分析:BootStrap
概览 BootStrap源码所在的位置是:org.apache.catalina.startup.Bootstrap 这个类是Tomcat项目的启动类,也就是main函数所在的地方,起始tomcat就 ...
- Maven项目Update Project...后JRE System Library会自动变回1.5解决办法
<build> <finalName>pay</finalName> <plugins> <plugin> <groupId>o ...
- Android 你可能忽略的提高敲代码效率的方式 (转)
每日推荐 Eyepetizer-in-Kotlin:一款简约的小视频app,带你走进kotlin 作为学习kotlin的一款app,在撸代码的过程中学习kotlin的语法及特性. Eyepetizer ...
- jmeter java 请求 payload
1.注册页面抓包看见内容如下: POST http://test.nnzhp.cn/bbs/forum.php?mod=post&action=edit&extra=&edit ...
- 创建有提示的ui组件
using UnityEditor; using UnityEngine; using System.Collections; using Edelweiss.CloudSystem; namespa ...
- window 怎么样让nginx开机自启动
安装Nginx 下载windows版nginx (http://nginx.org/download/nginx-1.10.0.zip),之后解压到需要放置的位置(D:\xampp\nginx) 将N ...
- Android 适配(drawable文件夹)图片适配(二)
参考自(https://blog.csdn.net/myoungmeng/article/details/54090891) Android资源文件存放: android的drawable文件一共可以 ...