希尔排序 Java实现
来源:互联网 发布:烟雾视频软件 编辑:程序博客网 时间:2024/05/17 06:20
希尔排序实际上是针对插入排序的一种优化算法。当被插入的元素要移动到数组的开始位置时需要比较的次数相当大,通过改变相邻的元素比较为距离间隔为h的元素比较(h逐渐减小为1)就能减少比较的次数。
代码如下:
public class Tester { private static final int N = 10; public static void main(String[] args){ Random random = new Random(); int[] t = new int[N]; for(int i=0;i<N;i++){ t[i]=random.nextInt(80); } shellSort(t); for(int a :t){ System.out.print(a+"\t"); } } /* * 将数组中下标为i与下标为k的数进行交换 */ public static void swap(int[] t,int i,int j){ int temp = t[i]; t[i]=t[j]; t[j]=temp; } public static void shellSort(int[] t){ int length = t.length; int h=1; while(h<length){h=h*3+1;} while(h>=1){ for(int i=h;i<length;i++){ //进行插入排序 t[j] t[j-h] t[j-2h]... for(int j=i;j>=h && t[j]<t[j-h];j-=h){ swap(t,j,j-h); } } h=h/3; } }}
阅读全文
0 0
- 希尔排序[Java实现]
- Java实现希尔排序
- 希尔排序----java实现
- 希尔排序--Java实现
- 希尔排序 java实现
- java实现希尔排序
- 希尔排序Java实现
- 希尔排序 java实现
- Java实现希尔排序
- java实现希尔排序
- 希尔排序 Java实现
- 希尔排序(Java实现)
- 希尔排序 java实现
- 希尔排序 Java实现
- java 实现希尔排序
- 希尔排序 Java实现
- 希尔排序-JAVA实现
- java实现希尔排序
- MFC自定义消息
- 354. Russian Doll Envelopes
- B
- AVL树与红黑树比较
- win7使用apache2作文件服务器
- 希尔排序 Java实现
- 从今以往 勿复相思
- 普通摄像头的数据输出格式YUV与mjpeg之间联系、DCT离散余弦变换去噪跟压缩
- 消息队列选型
- 【codevs 1075】明明的随机数
- 欢迎使用CSDN-markdown编辑器
- Python画图工具类库matplotlib
- OOXX Game
- [ROR]Single Table Inheritance with Rails 4 (Part 1)