/******************************************************************************
* Compilation: javac Shell.java
* Execution: java Shell < input.txt
* Dependencies: StdOut.java StdIn.java
* Data files: http://algs4.cs.princeton.edu/21elementary/tiny.txt
* http://algs4.cs.princeton.edu/21elementary/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 ]
*
*
******************************************************************************/ package edu.princeton.cs.algs4; /**
* The {@code Shell} 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 { private Shell() { } public static void sort(Comparable[] a) {
int n = a.length; int h = 1;
while (h < n/3) h = 3*h + 1; while (h >= 1) { 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);
} private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
} private static void exch(Object[] a, int i, int j) {
Object swap = a[i];
a[i] = a[j];
a[j] = swap;
} 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;
} 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;
} private static void show(Comparable[] a) {
for (int i = 0; i < a.length; i++) {
StdOut.println(a[i]);
}
} public static void main(String[] args) {
String[] a = StdIn.readAllStrings();
Shell.sort(a);
show(a);
} }

SHELL希尔排序的更多相关文章

  1. 希尔排序(Shell)

    希尔排序的实质就是分组插入排序,该方法又称缩小增量排序. 该方法的基本思想是:先将整个待排元素序列分割成若干个子序列(由相隔某个“增量”的元素组成的)分别进行直接插入排序,然后依次缩减增量再进行排序, ...

  2. 八大排序算法之二希尔排序(Shell Sort)

    希尔排序是1959 年由D.L.Shell 提出来的,相对直接排序有较大的改进.希尔排序又叫缩小增量排序 基本思想: 先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录 ...

  3. 插入排序与shell排序(希尔排序)

    1 .插入排序的过程如同我们平时打扑克牌取牌插入的过程,不断将取出的扑克牌插入已经排好的地方. 插入排序过程初始有序区间大小为1,取出无序区间的首元素,查找有序区间的合适位置,进行插入.不断重复上述过 ...

  4. [算法] 希尔排序 Shell Sort

    希尔排序(Shell Sort)是插入排序的一种,它是针对直接插入排序算法的改进.该方法又称缩小增量排序,因DL.Shell于1959年提出而得名. 希尔排序实质上是一种分组插入方法.它的基本思想是: ...

  5. 希尔排序(Shell's Sort)的C语言实现

    原创文章,转载请注明来自钢铁侠Mac博客http://www.cnblogs.com/gangtiexia 希尔排序(Shell's Sort)又称“缩小增量排序”(Diminishing Incre ...

  6. 希尔排序(shell‘ sort)

    希尔排序是1959 年由D.L.Shell 提出来的,相对直接排序有较大的改进.希尔排序又叫缩小增量排序 基本思想: 先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录 ...

  7. java排序算法(八):希尔排序(shell排序)

    java排序算法(八):希尔排序(shell排序) 希尔排序(缩小增量法)属于插入类排序,由shell提出,希尔排序对直接插入排序进行了简单的改进,它通过加大插入排序中元素之间的间隔,并在这些有间隔的 ...

  8. 八大排序算法——希尔(shell)排序(动图演示 思路分析 实例代码java 复杂度分析)

    一.动图演示 二.思路分析 希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序:随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,算法便终止. 简单插 ...

  9. 排序算法--希尔排序(Shell Sort)_C#程序实现

    排序算法--希尔排序(Shell Sort)_C#程序实现 排序(Sort)是计算机程序设计中的一种重要操作,也是日常生活中经常遇到的问题.例如,字典中的单词是以字母的顺序排列,否则,使用起来非常困难 ...

随机推荐

  1. Maven构建项目出现No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? 你应该 ...

  2. 英语进阶系列-A05-英语升级练习三

    古诗背诵 要求:认真背诵和朗读,然后翻译成现代文,并绘制图像描述图中的意向,时间限制到10 minutes.另外,从中找出英文单词,并记录. 例如:慈母 = kind mother,手 = hand, ...

  3. .NET CORE学习笔记系列(5)——ASP.NET CORE的运行原理解析

    一.概述 在ASP.NET Core之前,ASP.NET Framework应用程序由IIS加载.Web应用程序的入口点由InetMgr.exe创建并调用托管,初始化过程中触发HttpApplicat ...

  4. c# 7.0 6.0 新语法

    1.参考地址:https://docs.microsoft.com/zh-cn/dotnet/csharp/tutorials/exploration/csharp-7?tutorial-step=5 ...

  5. WebApi的版本控制

      using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; using Sy ...

  6. 第三章 启动rabbitmq的webUI

    一.启动步骤 1.启动rabbitmq rabbitmq-server (前台启动)或者rabbitmq-server -detached(后台启动) 2.启动rabbitmq_management ...

  7. [properJavaRDP]在网页中实现远程连接

    内容摘要: 利用开源软件properJavaRDP实现远程桌面连接 如何让Applet嵌入到网页中正常运行 如何处理连接win7时的异常:Wrong modulus size! Expected 64 ...

  8. c++ fmt 库安装和使用示例

    安装: 1 git clone  https://github.com/fmtlib/fmt.git 2. cmake . 3. make && make install  #incl ...

  9. mysql截取函数substring_index()和right()用法

    ); 以.作为截取的分隔符. ); 从第2位开始截取,截取所有的. );

  10. JavaScript实用的工具/类库

    Moment.js//JavaScript日期处理类库. Lodash.js//非常实用的工具库. MD5//加密. Numeral.js//格式化和数字四则运算. Pure.css//CSS模块.