希尔排序

来源:互联网 发布:湖南拼车软件 编辑:程序博客网 时间:2024/06/14 00:10

希尔排序是通过比较相距一定间隔的元素来工作的,也叫缩减增量排序

package chapter7;public class Demo2 {public static void main(String[] args) {Integer a[] = { 34, 8, 64, 80, 32, 21 };shellSort(a);}public static <Integer extends Comparable<? super Integer>> void shellSort(Integer[] a) {int j;for (int gap = a.length / 2; gap > 0; gap /= 2) {for (int i = gap; i < a.length; i++) {Integer temp = a[i];for (j = i; j >= gap && temp.compareTo(a[j - gap]) < 0; j -= gap) {a[j] = a[j - gap];}a[j] = temp;}}for (int i = 0; i < a.length; i++) {System.out.print(a[i] + " ");}}}


0 0
原创粉丝点击