希尔排序思想:使数组中随意间隔为h的元素都是有序的。

希尔排序是插入排序的优化。先对数组局部进行排序,最后再使用插入排序将部分有序的数组排序。

代码例如以下:

/**
*
* @author seabear
*
*/ public class ShellSort {
public static void sort(Comparable[] a)
{
int N = a.length;
int h = 1;
while(h < N/2)
{
h = 4 * 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)
{
int n = j - h;
System.out.println(j + ":" + a[j] + "," + n + ":" + a[j-h]);
exch(a,j,j-h);
show(a);
}
}
h = h / 4;
}
} public static boolean less(Comparable v,Comparable w)
{
return v.compareTo(w) < 0;
} public static void exch(Comparable[] a,int i,int j)
{
Comparable temp = a[i];
a[i] = a[j];
a[j] = temp;
} public static boolean isSort(Comparable[] a)
{
for(int i = 1; i < a.length; i++)
{
if(less(a[i],a[i-1]))
{
return false;
}
}
return true;
} public static void show(Comparable[] a)
{
for(int i = 0; i < a.length; i++)
{
System.out.print(a[i] + " ");
}
System.out.println();
} public static void main(String[] args)
{
Integer[] a = new Integer[10];
for(int i = 0; i < 10; i++)
{
a[i] = (int)(Math.random() * 10 + 1);
System.out.print(a[i] + " ");
}
System.out.println();
sort(a);
show(a);
}
}

算法(第四版)学习笔记之java实现希尔排序的更多相关文章

  1. 算法(第四版)学习笔记之java实现可以动态调整数组大小的栈

    下压(LIFO)栈:可以动态调整数组大小的实现 import java.util.Iterator; public class ResizingArrayStack<Item> imple ...

  2. 算法第四版学习笔记之优先队列--Priority Queues

    软件:DrJava 参考书:算法(第四版) 章节:2.4优先队列(以下截图是算法配套视频所讲内容截图) 1:API 与初级实现 2:堆得定义 3:堆排序 4:事件驱动的仿真 优先队列最重要的操作就是删 ...

  3. 算法第四版学习笔记之快速排序 QuickSort

    软件:DrJava 参考书:算法(第四版) 章节:2.3快速排序(以下截图是算法配套视频所讲内容截图) 1:快速排序 2:

  4. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十三)之Strings

    Immutable Strings Objects of the String class are immutable. If you examine the JDK documentation fo ...

  5. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(四)之Operators

    At the lowest level, data in Java is manipulated using operators Using Java Operators An operator ta ...

  6. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(三)之Everything Is an Object

    ---恢复内容开始--- Both C++ and Java are hybird languages. A hybird language allow multiple programming st ...

  7. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十四)之Type Information

    Runtime type information (RTTI) allow you to discover and use type information while a program is ru ...

  8. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十二)之Error Handling with Exceptions

    The ideal time to catch an error is at compile time, before you even try to run the program. However ...

  9. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十一)之Holding Your Objects

    To solve the general programming problem, you need to create any number of objects, anytime, anywher ...

随机推荐

  1. ASP.NET MVC HttpVerbs.Delete/Put Routes not firing

    原文地址: https://weblog.west-wind.com/posts/2015/Apr/09/ASPNET-MVC-HttpVerbsDeletePut-Routes-not-firing ...

  2. [转]Android使用Application总结

        目录(?)[+]   Application 配置全局Context 第一步.写一个全局的单例模式的MyApplication继承自Application 覆盖onCreate ,在这个方法里 ...

  3. ThinkPHP通过类的链式继承优化空操作的实现

    上篇<ThinkPHP空操作和空控制器的处理>中,在处理空操作时修改了父类Controller.class.php中代码,不到万不得已不能 修改基类控制器中的原码,此时可在子类与父类之间, ...

  4. GlobalGetAtomName GlobalDeleteAtom 引用 WinAPI: AddAtom、DeleteAtom、FindAtom、GetAtomName、GlobalAddAtom、GlobalDeleteAtom、GlobalFindAtom、GlobalGetAtomName

    http://www.cnblogs.com/del/archive/2008/02/28/1085124.html 这是储存字符串的一组 API.通过 AddAtom 储存一个字符串, 返回一个 I ...

  5. 【原】移动web资源整理(安卓、ios移动端兼容性问题归整)

     meta基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 <meta name="viewport" content="width=device-wi ...

  6. D-U-N-S申请流程

    1.打开D-U-N-S官网 http://fedgov.dnb.com/webform 图一 2.占击页面的“Click here to request your ......” (如图一红框所示)进 ...

  7. UI---------EventSystem

    创建uGUI控件后,Unity会同时创建一个叫EventSystem的GameObject,用于控制各类事件.可以看到Unity自带了两个Input Module,一个用于响应标准输入,一个用于响应触 ...

  8. 仿LOL项目开发第八天

    仿LOL项目开发第八天 by 草帽 这节我们继续上节所讲的内容,上节我们初始化好了LoginWindow,当我们点击确认选择服务器按钮的时候,就发送服务器id给游戏服务器. 这里就开始涉及到客户端需要 ...

  9. 《深入理解C指针》

    <深入理解C指针> 基本信息 原书名:Understanding and using C pointers 作者: (美)Richard Reese 译者: 陈晓亮 丛书名: 图灵程序设计 ...

  10. Python处理PDF及生成多层PDF

    Python提供了众多的PDF支持库,本文是在Python3环境下,试用了两个库来完成PDF的生成的功能.PyPDF对于读取PDF支持较好,但是没找到生成多层PDF的方法.Reportlab看起来更成 ...