算法(第四版)学习笔记之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 ...
随机推荐
- LeetCode:整数反转(Reserve Integer)
public class ReserveInteger { public int reverse(int x) { //用于接收个位数(10的余数) int remainder; //是否负数 int ...
- Polly简介 — 1. 故障处理策略
Polly 是 .Net 下的一套瞬时故障处理及恢复的函式库,可让开发者以fluent及线程安全的方式来应用诸如Retry.Circuit Breaker.Timeout.Bulkhead Isola ...
- run commands in linux shell using batch file
adb shell as root after device rooted once device rooted, we must perform "su" before we g ...
- C#线程安全的那些事
还是上一次,面试的时候提到了C#线程安全的问题,当时回答的记不太清了,大概就是多线程同是调用某一个函数时可能会照成数据发生混乱,运行到最后发现产生的结果或数据并不是自己想要的,或是跨线程调用属性或方法 ...
- DU 4609 3-idiots FFT
题意还是比较好懂. 给出若干个木棍的长度,问这些木棍构成三角形的可能性. 那么公式很容易知道 就是这些木棍组成三角形的所有情况个数 除以 从n个木棍中取3个木棍的情况数量C(n, 3) 即可 但是很显 ...
- MVC控制器传递多个Model到视图,使用ViewData, ViewBag, 部分视图, TempData, ViewModel, Tuple
从控制器传递多个Model到视图,可以通过ViewData, ViewBag, PartialView, TempData, ViewModel,Tuple等,本篇逐一体验.本篇源码在github. ...
- linux umask命令
umask命令 umask命令功能:显示.设置文件的缺省权限. umask命令语法:umask [-S] -S:以rwx形式显示新建文件或目录的缺省权限. 执行umask: 显示0022 第一个0:特 ...
- extjs表单验证
extjs表单验证 //放在onReady的function(){}中 Ext.QuickTips.init(); //为组件提供提示信息功能,form的主要提示信息就是客户端验证的错误信息. Ext ...
- kafka分区原理图
一个Topic的多个分区,被分布在kafka集群中的多个server上.每一个分区都有一个server为"leader";leader负责全部的读写操作,假设leader失效,那么 ...
- 使用sun.misc.BASE64Decoder出错解决方案
Access restriction: The type BASE64Decoder is not accessible due to restriction on required library ...