JAVA数据结构与算法之————希尔排序
个人理解:希尔排序其实是增量不为1的冒泡排序。
代码:
import java.util.Arrays;
/*
* 希尔排序算法的时间复杂度为O(n^1.5), 空间复杂度为O(1)
* */
public class ShellSort<E extends Comparable> {
private Swap<E> s = new Swap<>();
private void shellSort(E[] L){
/*增量*/
int increment = L.length;
int swap_count = 0;
int count = 0;
do{
increment = increment / 3 + 1;
for(int i = increment; i < L.length; i++){
if(L[i - increment].compareTo(L[i]) > 0){
s.swap(L, i - increment, i);
swap_count++;
}
}
}while(increment > 1);
System.out.println("swap_count:" + swap_count);
System.out.println("count:" + count);
}
public static void main(String[] args) {
ShellSort<Integer> shellSort = new ShellSort<>();
Integer[] data = {2, 4, 6, 1, 8, 2, 0, -1, 3, 10};
shellSort.shellSort(data);
System.out.println(Arrays.toString(data));
}
}