- package com.itheima;
- public class ShellSort
- {
- public static void main(String[] args)
- {
- int[] a =
- { 9, 8, 2, 6, 7, 3, 4, 1, 45, 78, 36, 45, 21, 12, 145, 1, 5 };
- int h = 1; // h变量保存可增变量
- int temp;
- int inner, outer;
- // 希尔排序
- long begin = System.currentTimeMillis();
- while (h <= a.length / 3)
- h = h * 3 + 1; // 按h*3+1得到增量序列的最大值
- while (h > 0)
- {
- for (outer = h; outer < a.length; outer++)
- {
- temp = a[outer];
- inner = outer;
- while (inner > h - 1 && a[inner - h] >= temp)
- {
- a[inner] = a[inner - h];
- inner -= h;
- }
- a[inner] = temp;
- }
- h = (h - 1) / 3;
- }
- for (int i = 0; i < a.length; i++)
- {
- System.out.println(a[i]);
- }
- }
- }
复制代码 |