算法(第四版)——04希尔排序
2016-11-11 17:16
141 查看
public class ShellSort { public static void main(String[] args) { int[] a = {45,67,43,28,90,15,77,88}; System.out.print("排序前的顺序:"); for (int i : a) { System.out.print(i + " "); } System.out.println(); System.out.println("排序过程:"); shellSort(a); System.out.print("排序后的顺序:"); for (int i : a) { System.out.print(i + " "); } } private static void shellSort(int[] a) { // TODO Auto-generated method stub int N = a.length; int h = 1; while(h < N/3) { h = h*3 + 1; } while(h>0) { for (int i = h; i < N; i++) { for (int j = i; j>=h && (a[j]<a[j-h]); j = j-h) { int temp = a[j]; a[j] = a[j-h]; a[j-h] = temp; for (int k : a) { System.out.print(k + " "); } System.out.println(); } } h = h/3; } } }
运行结果:
排序前的顺序:45 67 43 28 90 15 77 88
排序过程:
45 15 43 28 90 67 77 88
15 45 43 28 90 67 77 88
15 43 45 28 90 67 77 88
15 43 28 45 90 67 77 88
15 28 43 45 90 67 77 88
15 28 43 45 67 90 77 88
15 28 43 45 67 77 90 88
15 28 43 45 67 77 88 90
排序后的顺序:15 28 43 45 67 77 88 90
理解不充分,日后有时间再来记录
相关文章推荐
- C++primer(第四版)复习笔记—第二篇:容器和算法
- 算法-第四版-练习1.2.14解答
- 算法第四版第二章排序的总结
- 算法-第四版-1.3 背包、队列和栈-习题索引汇总
- 算法 第四版 习题 答案 笔记 1.1
- <C++Primer>第四版 阅读笔记 第二部分 “容器和算法”
- 一、算法第四版(二分查找、背包、队列、栈)
- 算法第四版第一章1.1.21题目小结
- 算法-第四版-练习1.3.23解答
- 算法-第四版-练习1.3.30解答
- 算法-第四版-练习1.3.7解答
- 算法-第四版-练习1.2.14解答
- 算法 第四版 1.3.39 环形缓冲区
- 算法 第四版 1.4.12
- 算法(第四版)——03插入排序
- 算法第四版书的C++版本代码
- 算法(第四版) 1.3答案
- 算法 第四版 2.3.9
- 《算法(第四版)》排序-----优先队列
- 算法(第四版)1.4摘抄