Hill sort idea: the elements that make an arbitrary interval of h in an array are ordered. The hill sort is an optimization of the insertion sort, first sorting the array locally, and then sorting the partially ordered arrays using the insert sort.
The code is as follows:
/** * * @author seabear * */public class Shellsort {public static void sort (comparable[] a) {int N = A.length;int h = 1;wh Ile (H < N/2) {h = 4 * H + 1;} while (H >= 1) {for (int i = h, i < N; i++) {for (int j = i, J >= H && less (A[j],a[j-h]), J-= h) {int N = J- H System.out.println (j + ":" + a[j] + "," + N + ":" + a[j-h]); Exch (a,j,j-h); show (A);}} h = H/4;}} public static Boolean less (comparable v,comparable W) {return V.compareto (W) < 0;} public static void Exch (comparable[] A,int I,int j) {Comparable temp = a[i];a[i] = a[j];a[j] = temp;} public static Boolean Issort (Comparable[] a) {for (int i = 1; i < A.length, i++) {if (less (a[i],a[i-1])) {return false;}} return true;} public static void Show (comparable[] a) {for (int i = 0; i < a.length; i++) {System.out.print (A[i] + "");} System.out.println ();} public static void Main (string[] args) {integer[] a = new Integer[10];for (int i = 0; i <; i++) {a[i] = (int) (Math.rand Om () * 10 + 1); System.out.print (A[i] + "");} System.out.prinTLN (); sort (a); show (A);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Algorithm (fourth edition) Java implementation of learning Notes Hill sort