算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)
一、介绍
1.希尔排序的思路:希尔排序是插入排序的改进。当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为插入排序元素只能一位一位地交换。基于插入排序的这个缺点,希尔排序是以一个区间来的交换元素,然后不断缩小区间,直到为1。它的理论是,每次经过一个区间的排序后,元素就会更有序些,所以下一个区间排序时,要交换的元素次数会变少。
2.详细的数据排序交换过程
3.
Proposition.
The number of compares used by shellsort with the increments 1, 4, 13, 40, 121, 364, ... is O(N3/2).
二、代码
package algorithms.elementary21; import algorithms.util.StdIn;
import algorithms.util.StdOut; /******************************************************************************
* Compilation: javac Shell.java
* Execution: java Shell < input.txt
* Dependencies: StdOut.java StdIn.java
* Data files: http://algs4.cs.princeton.edu/21sort/tiny.txt
* http://algs4.cs.princeton.edu/21sort/words3.txt
*
* Sorts a sequence of strings from standard input using shellsort.
*
* Uses increment sequence proposed by Sedgewick and Incerpi.
* The nth element of the sequence is the smallest integer >= 2.5^n
* that is relatively prime to all previous terms in the sequence.
* For example, incs[4] is 41 because 2.5^4 = 39.0625 and 41 is
* the next integer that is relatively prime to 3, 7, and 16.
*
* % more tiny.txt
* S O R T E X A M P L E
*
* % java Shell < tiny.txt
* A E E L M O P R S T X [ one string per line ]
*
* % more words3.txt
* bed bug dad yes zoo ... all bad yet
*
* % java Shell < words3.txt
* all bad bed bug dad ... yes yet zoo [ one string per line ]
*
*
******************************************************************************/ /**
* The <tt>Shell</tt> class provides static methods for sorting an
* array using Shellsort with Knuth's increment sequence (1, 4, 13, 40, ...).
* <p>
* For additional documentation, see <a href="http://algs4.cs.princeton.edu/21elementary">Section 2.1</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class Shell { // This class should not be instantiated.
private Shell() { } /**
* Rearranges the array in ascending order, using the natural order.
* @param a the array to be sorted
*/
public static void sort(Comparable[] a) {
int N = a.length; // 3x+1 increment sequence: 1, 4, 13, 40, 121, 364, 1093, ...
int h = 1;
while (h < N/3) h = 3*h + 1; while (h >= 1) {
// h-sort the array
for (int i = h; i < N; i++) {
for (int j = i; j >= h && less(a[j], a[j-h]); j -= h) {
exch(a, j, j-h);
}
}
assert isHsorted(a, h);
h /= 3;
}
assert isSorted(a);
} /***************************************************************************
* Helper sorting functions.
***************************************************************************/ // is v < w ?
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
} // exchange a[i] and a[j]
private static void exch(Object[] a, int i, int j) {
Object swap = a[i];
a[i] = a[j];
a[j] = swap;
} /***************************************************************************
* Check if array is sorted - useful for debugging.
***************************************************************************/
private static boolean isSorted(Comparable[] a) {
for (int i = 1; i < a.length; i++)
if (less(a[i], a[i-1])) return false;
return true;
} // is the array h-sorted?
private static boolean isHsorted(Comparable[] a, int h) {
for (int i = h; i < a.length; i++)
if (less(a[i], a[i-h])) return false;
return true;
} // print array to standard output
private static void show(Comparable[] a) {
for (int i = 0; i < a.length; i++) {
StdOut.println(a[i]);
}
} /**
* Reads in a sequence of strings from standard input; Shellsorts them;
* and prints them to standard output in ascending order.
*/
public static void main(String[] args) {
String[] a = StdIn.readAllStrings();
Shell.sort(a);
show(a);
} }
算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)的更多相关文章
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)
一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)
一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)
一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursivel ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版
package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)
一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-008排序算法的复杂度(比较次数的上下限)
一. 1. 2.
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-003比较算法及算法的可视化
一.介绍 1. 2. 二.代码 1. package algorithms.elementary21; /*********************************************** ...
- 算法Sedgewick第四版-第1章基础-001递归
一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...
- 算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的
1. package algorithms.stacks13; /******************************************************************* ...
随机推荐
- gradle_学习_01_gradle安装与基本使用
一.下载安装 1.下载地址 官方下载地址: https://gradle.org/install/#manually 下载二进制文件,解压即可 2.环境变量配置 加入以下环境变量 GRADLE_HOM ...
- JDBC操作简单实用了IOUtils
package cn.itcast.demo4; import java.io.FileInputStream; import java.io.FileOutputStream; import jav ...
- python3 xrange报错
因为python3取消了xrange这个函数,range就是xrange.
- RedHat5.8 编译内核驱动 合成initrd.img
/******************************************************************* * RedHat5.8 编译内核驱动 合成initrd.img ...
- THUPC2017 小 L 的计算题
求 $k=1,2,\cdots,n \space \space \sum\limits_{i=1}^n a_i^k$ $n \leq 2 \times 10^5$ sol: 时隔多年终于卡过去了 之前 ...
- java06-数组动手动脑
1.阅读QiPan.java示例程序了解如何利用二维数组和循环语句绘制五子棋盘. 定义了一个私有的二维数组作为棋盘.并定义了长度.之后打印符号使之连接起来作为棋盘在控制台显示.建立缓冲区用来读取输入的 ...
- Python 2.7_爬取妹子图网站单页测试图片_20170114
1.url= http://www.mzitu.com/74100/x,2为1到23的值 2.用到模块 os 创建文件目录; re模块正则匹配目录名 图片下载地址; time模块 限制下载时间;req ...
- linux内核图形配置疑难解决
配置linux内核是遇到的问题:(1)问题一make gconfig * * Unable to find the GTK+ installation. Please make sure that * ...
- SVN 命令使用-***
1.检出到某一版本: svn checkout -r 974 svn://220.231.xx.xx/仓库名 svn checkout svn://220.231.xx.xx/仓库名 svn ex ...
- Jlink flash 烧录HEX 程序
一般Jlink版本 和 Jag(硬件)最好匹配 安装Jlink 时,IAR的工具包也可以顺带安装. 有源码: IAR 可以自动选择CPU型号,代码直接Download and debug https: ...