算法(第四版)学习笔记之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 ...
随机推荐
- with在模板中的应用
var str = 'Hello <%= name %>!'; var o = { name: 'Alice' }; function tmpl(str, obj) { str = 'va ...
- [Assembly]汇编编写简易键盘记录器
环境:Windows xp sp3工具:masmnotepad++ 首先列出本次编程程序要执行的步骤:(1).读取键盘所输入的字符(2).输出到屏幕上(3).完善Esc.Backspace.空格.回车 ...
- bzoj 3545/3551: [ONTAK2010]Peaks -- 主席树,最小生成树,倍增
3545: [ONTAK2010]Peaks Time Limit: 10 Sec Memory Limit: 128 MB Description 在Bytemountains有N座山峰,每座山峰 ...
- Loj10166 数字游戏2
题目描述 由于科协里最近真的很流行数字游戏,某人又命名了一种取模数,这种数字必须满足各位数字之和 modN 为 000.现在大家又要玩游戏了,指定一个整数闭区间 [a,b][a,b][a,b],问这个 ...
- Codeforces Round #359 (Div. 1) B. Kay and Snowflake dfs
B. Kay and Snowflake 题目连接: http://www.codeforces.com/contest/685/problem/B Description After the pie ...
- 文件上传demo
前端代码: <form action="upload.php" enctype="multipart/form-data" method="po ...
- 【原】Java程序调用远程Shell脚本
此程序的目的是执行远程机器上的Shell脚本. [环境参数]远程机器IP:192.168.234.123用户名:root密码:rootShell脚本的路径:/home/IFileGenTool/Bak ...
- NBT(NetBIOS Over TCP)名称解析概述
在微软IP网络中,客户计算机查找其他计算机并与之进行通信的主要手段是利用域名(DNS).但是,使用先前版本的Windows户机也使用NetBIOS协议,将名称解析为IP地址. 通过三种方法解析NetB ...
- 华为S5300系列升级固件S5300SI-V100R005C00SPC100.cc
这个固件附带了web,注意,这个插件是升级V200的必经固件,所以必须升级为此固件之后才能往下升级. 升级小插曲: 1.升级的使用使用Windows,不要用Mac或者Linux,因为从Mac/Linu ...
- Android性能优化之渲染
Google近期在Udacity上发布了Android性能优化的在线课程,目前有三个篇章,分别从渲染,运算与内存,电量三个方面介绍了如何去优化性能,这些课程是Google之前在Youtube上发布的A ...