一、什么是希尔排序?
二、java实现
package myStudy.dataStructure.day9;
/**
*
* @description 希尔排序
* @author shenrenfeng
* @date 2018年11月21日 下午8:35:58
*
*/
public class ShellSort {
public static void main(String[] args) {
Integer[] array = {0,8,2,3,5,4,6,7,1,9};
array = shellSort(array);
for(int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
public static Integer[] shellSort(Integer[] array) {
int h = 1;
while(h < array.length) {// 计算希尔排序的间隔
h = 3*h + 1;
}
while(h > 1) {
h = (h - 1) / 3;
for(int i = 0; i < array.length; i++) {// 进行插入排序
for(int j = i; j < array.length - h; j = j + h) {
if(array[j+h] < array[i]) {
int temp = array[i];
array[i] = array[j+h];
array[j+h] = temp;
}
}
}
}
return array;
}
}