算法(第四版)学习笔记之java实现希尔排序
希尔排序思想:使数组中随意间隔为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实现希尔排序的更多相关文章
- 算法(第四版)学习笔记之java实现可以动态调整数组大小的栈
下压(LIFO)栈:可以动态调整数组大小的实现 import java.util.Iterator; public class ResizingArrayStack<Item> imple ...
- 算法第四版学习笔记之优先队列--Priority Queues
软件:DrJava 参考书:算法(第四版) 章节:2.4优先队列(以下截图是算法配套视频所讲内容截图) 1:API 与初级实现 2:堆得定义 3:堆排序 4:事件驱动的仿真 优先队列最重要的操作就是删 ...
- 算法第四版学习笔记之快速排序 QuickSort
软件:DrJava 参考书:算法(第四版) 章节:2.3快速排序(以下截图是算法配套视频所讲内容截图) 1:快速排序 2:
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十三)之Strings
Immutable Strings Objects of the String class are immutable. If you examine the JDK documentation fo ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(四)之Operators
At the lowest level, data in Java is manipulated using operators Using Java Operators An operator ta ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(三)之Everything Is an Object
---恢复内容开始--- Both C++ and Java are hybird languages. A hybird language allow multiple programming st ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 解耦你的HTML,CSS和JAVASRIPT
注:本文为翻译文章,原文<Decoupling Your HTML, CSS, and JavaScript> 今天在web上任何大一点的网站或应用程序都包含大量的html,css和jav ...
- 【BZOJ】4152: [AMPPZ2014]The Captain【SLF优化Spfa】
4152: [AMPPZ2014]The Captain Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 2107 Solved: 820[Submi ...
- Codeforces Round #356 (Div. 2) D. Bear and Tower of Cubes dfs
D. Bear and Tower of Cubes 题目连接: http://www.codeforces.com/contest/680/problem/D Description Limak i ...
- poj 1330 Nearest Common Ancestors 单次LCA/DFS
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19919 Accept ...
- Linux性能监控分析命令(二)—sar命令介绍
性能监控分析的命令包括如下: 1.vmstat 2.sar 3.iostat 4.top 5.free 6.uptime 7.netstat 8.ps 9.strace 10.lsof ======= ...
- mysql数据库cup飙升处理思路
1.先top查看是那一个进程,哪个端口占用CPU多. 2.show processeslist查看是否由于大量并发,锁引起的负载问题. 3.否则,查看慢查询,找出执行时间长的sql:explain分析 ...
- [JAVA] JAVA JDK 安装配置
JDK 安装 下载安装 下载JDK 从oracle官方网站下载并安装JDK. 下载使用文档 从oracle官方网站下载使用帮助文档. 安装库源文件 源文件位于安装目录的 /Library/Java/J ...
- 主动找智能钥匙 PKE取代RKE是大势所趋
http://taobao.autos.cn.yahoo.com/ypen/20111128/725214.html 近日,在车友论坛上的一个热帖<悲喜交加:1分钟就能复制汽车遥控器?>在 ...
- WebSocket重连reconnecting-websocket.js的使用
原文:https://www.cnblogs.com/kennyliu/p/6477746.html 页面引用 <script src="~/Scripts/reconnectin ...
- 【甘道夫】通过Mahout构建推荐系统--通过IDRescorer扩展评分规则
通过Mahout构建推荐系统时,假设我们须要添�某些过滤规则(比方:item的创建时间在一年以内),则须要用到IDRescorer接口,该接口源代码例如以下: package org.apache.m ...