Code from a code cloud
public static void Shellsort (int[] arr) {//Initialize incrementInth = 1;//Calculate maximum interval, formula: H = h * 3 + 1While (H < ARR.LENGTH/3) { h = H * 3 + 1; }//Zoom out increments for sortingWhile (h > 0) { //Insert Sortint Waitinsert;//wait for the number of insertionsint i,j;//i Indicates the current number of subscripts to be inserted, and J indicates the ordinal position of the comparison.For (i = h; i < arr.length; i++) { Waitinsert = Arr[i];//Get the number to be inserted in this roundj = i-h; //Compare position initialization, which is the last position of the ordered sequence, from the back forward //If it is greater than or equal to the size of the value waiting to be inserted, the number is shifted to the right one interval size, then the next comparisonWhile (J >-1 && arr[j] >= waitinsert) {Arr[j +h] = arr[j]; j = J-h;}//The location of the insertion must be the position of the last comparison, that is, the location of the j+h. (Note the timing of the j-h to understand)Arr[j +h] = Waitinsert; }//Zoom out increment, formula: h = (h-1)/3h = (h-1)/3; } }
Java Hill Sort