一、介绍

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)的更多相关文章

  1. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)

    一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...

  2. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)

    一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...

  3. 算法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 ...

  4. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版

    package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...

  5. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)

    一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...

  6. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-008排序算法的复杂度(比较次数的上下限)

    一. 1. 2.

  7. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-003比较算法及算法的可视化

    一.介绍 1. 2. 二.代码 1. package algorithms.elementary21; /*********************************************** ...

  8. 算法Sedgewick第四版-第1章基础-001递归

    一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...

  9. 算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的

    1. package algorithms.stacks13; /******************************************************************* ...

随机推荐

  1. 省略setget方法

    可以装一下这个插件再引入一个jar包实体类里不需要再写get/set方法了 maven坐标:<dependency> <groupId>org.projectlombok< ...

  2. 【整理】2-SAT

    2-satisfiability,我们一般将其缩写为 2-sat. 了解全名有助于我们对这个算法的理解.     百度翻译:‘satisfiability’---“可满足性,适定性”. “合取范式可满 ...

  3. an easy problem(贪心)

    An Easy Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8333   Accepted: 4986 D ...

  4. ASP.net之HttpModel

    HttpModule是向实现类提供模块初始化和处置事件.当一个HTTP请求到达HttpModule时,整个ASP.NET Framework系统还并没有对这个HTTP请求做任何处理,也就是说此时对于H ...

  5. 使用dumpbin命令查看dll导出函数及重定向输出到文件【轉】

    查看dll导出函数,一般使用Viewdll等第三方工具. VS开发环境中,可以查看32位和64位的dll.具体使用方法如下: 1. 进入VS开发环境,然后Tools -> Visual stud ...

  6. CentOS 7 安装Percona,Xtrabackup

    CentOS 7 安装Percona 5.7,Xtrabackup 简介 Percona Server为 MySQL 数据库服务器进行了改进,在功能和性能上较 MySQL 有着很显著的提升.该版本提升 ...

  7. hihoCoder#1139(二分+bfs)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回和上上回里我们知道Nettle在玩<艦これ>,Nettle在整理好舰队之后终于准备出海捞船和敌军交战了 ...

  8. MyBatis 批量插入数据对插入记录数的限制

    <基于 MyBatis 框架的批量数据插入的性能问题的探讨>(作者:魏静敏 刘欢杰 来源:<计算机光盘软件与应用> 2013 年第 19 期)中提到批量插入的记录数不能超过10 ...

  9. GUI练习中

    总结:JFrame和Frame是有很大差别的. 不要混淆.否则方法是不能成功调用的 特别是背景色:JFrame.对象f在main里无法调用背景色前景色都不想显示 一下是书上的一段代码,编译错误,但是可 ...

  10. codeforce 103B Cthulhu

    B. Cthulhu time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...