Hill Sort Summary
The hill sort is based on the following two-point nature of the insertion sort, which proposes an improved method:
- The insertion sort is efficient in the case of almost sequenced data operations, i.e. the efficiency of linear sequencing can be achieved. (The hill sort first sorts some of the data, which is the equivalent of a partial order.)
- But the insertion sort is generally inefficient because the insertion sort can only move data one bit at a time. (The hill sort first shifts the large interval data, and the later shift distances are relatively small)
Analysis:
First the first increment of an integer less than n, the entire record of the file is grouped. All records with a multiple of the distance h are placed in the same family, followed by a direct insert sort within each group, and then the second increment repeats the above groupings and sorts until the increment is 1.
Example: For the 7, 10, 1, 9, 2, 5, 8, 6, 4, 3 arrays, an increment of 4 for the hill sort process, the array can be considered to consist of 4 words group (0, 4, 8) (1, 5, 9) (2, 6) (3, 7).
If the increment is 1, then the entire array is inserted into the sort.
The advantage of the hill sort is that it reduces the amount of duplication and movement of the data that is being inserted, and also improves the efficiency greatly.
Implementation code:
Hill Sort class:
Package Com.dxx.order;import Java.util.arraylist;public class Shellsort {private int arrs[];//set an incremental array with the formula H=3*h+1,eg:1, 4, 13, 40 ... find a close array length but less than its increment private arraylist<integer> rises = new arraylist<integer> ();p ublic Shellsort (int[] arrs) {super (); this.arrs = Arrs;} Defines the delta array public void setrises () {int len = arrs.length;int h = 1;int Count=0;while (true) {if (H>=len) {break;} Rises.add (h); h = 2*h + 1;}} public void Sortarrs () {int h,temp,len = rises.size ();//Sort by increment sequentially for (int i=0; i<len;i++) {h = rises.get (len-1-i);// Sorts an array with an increment of H arrs[h]for (int k=h;k<arrs.length;k++) {//Sort an array with an increment of h in a bubbling sort if (arrs[k]<arrs[k-h]) {temp = Arrs [K];while (K>=h && temp<arrs[k-h]) {arrs[k] = arrs[k-h];k-=h;} Arrs[k] = Temp;k+=h;}}} public void SortArrs2 () {int group, I, J, temp;int len = arrs.length; for (group = LEN/2; group > 0, Group/= 2) {for (i = group; i < Len; i++) { for (j = i-group; J >= 0; J-= Group){if (Arrs[j] > arrs[j + Group]) {temp = Arrs[j]; ARRS[J] = arrs[j + Group]; Arrs[j + Group] = temp; }}}}}public void Printarrs () {for (int i:arrs) {System.out.print (i + "");} System.out.println ();}}
Main Program class:
Package Com.dxx.order;public class Maintest {public static void main (string[] args) {int arrs[] = {14,3,2,5,12,8,6,7,10,11 , 1,9,13}; Shellsort shellsort = new Shellsort (ARRS); Shellsort.printarrs (); shellsort.setrises (); Shellsort.sortarrs (); Shellsort.printarrs ();}}
Hill sort Java