希尔排序的诞生是由于插入排序在处理大规模数组的时候会遇到需要移动太多元素的问题。希尔排序的思想是将一个大的数组“分而治之”,划分为若干个小的数组,以 gap 来划分,比如数组 [1, 2, 3, 4, 5, 6, 7, 8] ,如果以 gap = 2 来划分,可以分为 [1, 3, 5, 7] 和 [2, 4, 6, 8] 两个数组(对应的,如 gap = 3 ,则划分的数组为: [1, 4, 7] 、 [2, 5, 8] 、 [3, 6] )然后分别对划分出来的数组进行插入排序,待各个子数组排序完毕之后再减小 gap 值重复进行之前的步骤,直至 gap = 1 ,即对整个数组进行插入排序,此时的数组已经基本上快排好序了,所以需要移动的元素会很小很小,解决了插入排序在处理大规模数组时较多移动次数的问题。

具体实例请参照插入排序。

希尔排序是插入排序的改进版,在数据量大的时候对效率的提升帮助很大,数据量小的时候建议直接使用插入排序就好了。

实现代码:

  1. public <T extends Comparable<T>> void sort(T[] array, boolean ascend) {
  2. int length = array.length;
  3. int gap = 1;
  4. while (gap < length / 3) {
  5. gap = gap * 3 + 1;
  6. }
  7. while (gap >= 1) {
  8. for (int i = gap; i < length; i++) {
  9. T next = array[i];
  10. int j = i;
  11. while (j >= gap) {
  12. int compare = array[j - gap].compareTo(next);
  13. if (compare == 0 || compare < 0 == ascend) {
  14. break;
  15. }
  16. array[j] = array[j - gap];
  17. j -= gap;
  18. }
  19. if (j != i) {
  20. array[j] = next;
  21. }
  22. }
  23. gap /= 3;
  24. }
  25.  
  26. }

例2:

  1. package org.rut.util.algorithm.support;
  2.  
  3. import org.rut.util.algorithm.SortUtil;
  4.  
  5. public class ShellSort implements SortUtil.Sort{
  6.  
  7. /* (non-Javadoc)
  8. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  9. */
  10. public void sort(int[] data) {
  11. for(int i=data.length/2;i>2;i/=2){
  12. for(int j=0;j<i;j++){
  13. insertSort(data,j,i);
  14. }
  15. }
  16. insertSort(data,0,1);
  17. }
  18.  
  19. /**
  20. * @param data
  21. * @param j
  22. * @param i
  23. */
  24. private void insertSort(int[] data, int start, int inc) {
  25. int temp;
  26. for(int i=start+inc;i<data.length;i+=inc){
  27. for(int j=i;(j>=inc)&&(data[j]<data[j-inc]);j-=inc){
  28. SortUtil.swap(data,j,j-inc);
  29. }
  30. }
  31. }
  32.  
  33. }
  1. SortUtil
  2.  
  3. package org.rut.util.algorithm;
  4.  
  5. import org.rut.util.algorithm.support.BubbleSort;
  6. import org.rut.util.algorithm.support.HeapSort;
  7. import org.rut.util.algorithm.support.ImprovedMergeSort;
  8. import org.rut.util.algorithm.support.ImprovedQuickSort;
  9. import org.rut.util.algorithm.support.InsertSort;
  10. import org.rut.util.algorithm.support.MergeSort;
  11. import org.rut.util.algorithm.support.QuickSort;
  12. import org.rut.util.algorithm.support.SelectionSort;
  13. import org.rut.util.algorithm.support.ShellSort;
  14.  
  15. public class SortUtil {
  16. public final static int INSERT = 1;
  17. public final static int BUBBLE = 2;
  18. public final static int SELECTION = 3;
  19. public final static int SHELL = 4;
  20. public final static int QUICK = 5;
  21. public final static int IMPROVED_QUICK = 6;
  22. public final static int MERGE = 7;
  23. public final static int IMPROVED_MERGE = 8;
  24. public final static int HEAP = 9;
  25.  
  26. public static void sort(int[] data) {
  27. sort(data, IMPROVED_QUICK);
  28. }
  29. private static String[] name={
  30. "insert", "bubble", "selection", "shell", "quick", "improved_quick", "merge", "improved_merge", "heap"
  31. };
  32.  
  33. private static Sort[] impl=new Sort[]{
  34. new InsertSort(),
  35. new BubbleSort(),
  36. new SelectionSort(),
  37. new ShellSort(),
  38. new QuickSort(),
  39. new ImprovedQuickSort(),
  40. new MergeSort(),
  41. new ImprovedMergeSort(),
  42. new HeapSort()
  43. };
  44.  
  45. public static String toString(int algorithm){
  46. return name[algorithm-1];
  47. }
  48.  
  49. public static void sort(int[] data, int algorithm) {
  50. impl[algorithm-1].sort(data);
  51. }
  52.  
  53. public static interface Sort {
  54. public void sort(int[] data);
  55. }
  56.  
  57. public static void swap(int[] data, int i, int j) {
  58. int temp = data<i>;
  59. data<i> = data[j];
  60. data[j] = temp;
  61. }
  62. }

Java算法-希尔排序的更多相关文章

  1. C数据结构排序算法——希尔排序法用法总结(转http://www.cnblogs.com/skywang12345/p/3597597.html)

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

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

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

  3. 数据结构和算法(Golang实现)(22)排序算法-希尔排序

    希尔排序 1959 年一个叫Donald L. Shell (March 1, 1924 – November 2, 2015)的美国人在Communications of the ACM 国际计算机 ...

  4. 使用 js 实现十大排序算法: 希尔排序

    使用 js 实现十大排序算法: 希尔排序 希尔排序 refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

  5. 排序算法-希尔排序(Java)

    package com.rao.sort; import java.util.Arrays; /** * @author Srao * @className ShellSort * @date 201 ...

  6. 排序算法:Java实现希尔排序

    希尔排序的思路是先分组再整合 先对下标进行分组,比如当数组长度为20时,一开始选定一个间隔值为10 对数组进行排序,每隔10个元素比较大小并交换,以下标为间隔,1和11比较.2和12比较......1 ...

  7. 算法(第四版)学习笔记之java实现希尔排序

    希尔排序思想:使数组中随意间隔为h的元素都是有序的. 希尔排序是插入排序的优化.先对数组局部进行排序,最后再使用插入排序将部分有序的数组排序. 代码例如以下: /** * * @author seab ...

  8. Java实现希尔排序

            华杰让我看了一道面试题:现有一段程序S,可以对任意n个数进行排序.如果现在需要对n^2个数进行排序,最少需要调用S多少次?(只允许调用S,不可以做别的操作).         看到了这 ...

  9. JAVA数据结构--希尔排序

    希尔排序通过将比较的全部元素分为几个区域来提升插入排序的性能.这样可以让一个元素可以一次性地朝最终位置前进一大步.然后算法再取越来越小的步长进行排序,算法的最后一步就是普通的插入排序,但是到了这步,需 ...

随机推荐

  1. 边工作边刷题:70天一遍leetcode: day 85-1

    Inorder Successor in BST 要点:这题要注意的是如果不是BST,没法从树结构上从root向那边找p,只能遍历.而根据BST,可以只走正确方向 如果不检查right子树,可以从ro ...

  2. UVA 11992 Fast Matrix Operations (二维线段树)

    解法:因为至多20行,所以至多建20棵线段树,每行建一个.具体实现如下,有些复杂,慢慢看吧. #include <iostream> #include <cstdio> #in ...

  3. Android组件系列----Android Service组件深入解析

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  4. java 16-6 泛型

    ArrayList存储字符串并遍历 我们按照正常的写法来写这个程序, 结果确出错了. 为什么呢? 因为我们开始存储的时候,存储了String和Integer两种类型的数据. 而在遍历的时候,我们把它们 ...

  5. 书籍推荐 《移动Web手册》 奇舞团

    书籍推荐  <移动Web手册> 奇舞团

  6. Sublime Text2 新建文件快速生成Html头部信息和炫酷的代码补全

    预备:安装emmet插件(previously known as Zen Coding) 方法一  package control法: 上一篇博客已经介绍了如何安装package control.打开 ...

  7. 【转】【WPF】资源读取 URI

    一开始看到WPF里面经常用如下语句来构造资源文件Uri: Uri uri = new Uri("/AssemblyName;component/image.png"); 我还以为这 ...

  8. [QoS]cisco3560限速配置案例-收集于网工泡泡

    网络中常用到这些:CISCO和H3C-MAC过滤+端口限速+端口镜像+端口隔离 不同的方式不同的思想:嘎嘎 其他各个厂商的限速链接:http://pan.baidu.com/s/1hrIMoSG 密码 ...

  9. TDD开发感悟

    由于公司要实现TDD形式的开发,所以准备了一下,准备在后续的项目中,投入到TDD的怀抱中. 在找一些参考书目的过程中,偶遇<测试驱动开发的艺术>这本书,书中的编码为JAVA派系,但是书的内 ...

  10. PC网站应用接入微信登录

    参考文档: https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&ve ...