package com.heu.wsq.basic.sort_algorithm;
public class ShellSort {
public void sort(int[] nums){
int n = nums.length;
int h = 1;
while(h < n/3){
h = h * 3 + 1;
}
while(h >= 1){
for(int i = h; i < n; i++){
for(int j = i; j >= h && less(nums[j], nums[j - h]); j -= h){
swap(nums, j, j - h);
}
}
h /= 3;
}
}
private boolean less(int num1, int num2){
return num1 < num2;
}
private void swap(int[] nums, int i, int j){
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
public static void main(String[] args) {
int[] arr = {5, 2, 1, 7, 9, 3, 8 ,2};
ShellSort shellSort = new ShellSort();
shellSort.sort(arr);
for(int i = 0; i < arr.length; i++){
System.out.print(i + ",");
}
}
}