An example of implementing hill sort in Java

  • 2020-04-01 02:26:41
  • OfStack

I. theoretical preparation
  Shell Sort is a kind of insertion Sort, which is an improvement on the direct insertion Sort algorithm. It is to divide the whole non-sequence into several small sub-sequences for insertion Sort, which is not stable. This method, also known as reduced delta sort, got its name from dl.shell, which was proposed in 1959.
Basic idea: take an integer less than n, d1, as the first increment, and divide all the records in the file into d1 groups. All records of distance multiples of d1 are placed in the same group. First, direct insertion sort is performed in each group. And then you take the second increment, d2< D1 repeat the above grouping and sorting until the increment dt=1(dt< Dt - l< ... < D2 < D1), that is, until all records are placed in the same group for direct insertion sort.
Reasons why the time performance of hill sort is better than that of direct insertion sort:
(1) when the file is basically in order when the initial state of direct insertion sort and the number of comparison and movement are less.
When n value is small, the difference between n and n2 is small, that is, the best time complexity O(n) and the worst time complexity 0(n2) of direct insertion sort are not much different.
(3) at the beginning of the increment in the hill sorting, grouping is more, the number of records in each group, so each group directly insert quickly, then increment di gradually narrowed, gradually reduce the number of clusters, the number of records and each group increase gradually, but having the di pai - 1 as a distance sequence, the file is close to the order, so the new sorting process faster.
  Therefore, the efficiency of hill sort is better than that of direct insertion sort.
Delta sequence selection: the Shell sort execution time depends on the delta sequence.
Common characteristics of a good sequence of deltas
The last increment must be 1;
Should try to avoid the sequence of values (especially adjacent values) each other's multiples of the case.          
Seeing this, I want to try hill sort, just learn it.

public class ShellSort {
 public static void main(String[] args) {

  int[] arr = new int[]{44,33,99,10,30,20,59,78,23,48};
  System.out.print(" Before ordering: ");
  for(int o: arr) {
   System.out.print(o+" ");
  }
  System.out.println();
  shellSort(arr);
  System.out.print(" After the order: ");
  for(int o: arr) {
   System.out.print(o+" ");
  }
  System.out.println();
 }
 private static void shellSort(int[] arr) {

  int j;
  int len = arr.length;
  for(int val=len>>1; val>0; val>>=1) {
   //Here is a direct insertion sort for all the groups this time
   for(int i=val; i<len; i++) {
    int temp = arr[i];
    
    for(j=i; j>=val&&temp<arr[j-val]; j-=val) {
     
     arr[j] = arr[j-val];
    }
    
    arr[j] = temp;
   }
  }
 }
}

Problem 3.
Does hill sort correctly? In other words, how to select the increment sequence to ensure the correct (including length, value)? Yes, the last time it's ok to just make sure the increment is 1 (regardless of the length of the sequence, but the efficiency is low), if the sequence is only 1, it's a direct insertion sort, I don't know.

Related articles: