java way to achieve Hill sorting

First, the basic idea and briefly Hill sorting

  Hill, also known as sort descending increments sorting algorithm is a more efficient insertion sort of an improved version. But Hill sorting non-stable sort algorithm. Hill sorting have the following general improvements than insertion sort:

  • Usually insertion sort can only move a data, and Hill sorting step in accordance with the movement (step = array length / 2), the step size is decreasing.

  The basic idea is to sort Hill: first dividing the entire sequence to be sorted into several sub-sequences direct insertion sort, to be substantially the entire recording sequence ordered, and then sequentially for all records direct insertion sort

Second, the basic steps

  1. The entire sequence is divided into n / 2 sub-sequence, i.e. the first k number with n / 2 + k (k <= n / 2) number of a sequence.
  2. The sequence portioned manner with respective insertion sort sorted sequence.
  3. The entire sequence is divided into n / 4 sub-sequences, sequences and good points sorted according to an insertion sort.
  4. Continue performing the above operation until a number of sub-sequences, the sort is complete.

Third, case studies

For example the sequence 1,5,4,30,6,29,5,20

First trip Sort: promoter sequence number = n / 2 = 4 for the four sub-sequence {1,6}, {5,29}, {4,5}, {30, 20} After the order of these four sequences { 1,5,4,20,6,29,5,30}.

Second trip Sort: promoter sequence number = n / 4 = 2 two sequences is {1,4,6,5}, {5,20,29,30} the order of these two sequences {1,5, 4,20,5,29,6,30}.

Second trip Sort: promoter sequence number = n / 8 = 1 a sub-sequence {1,4,6,5,5,20,29,30} and the sequence order of these two {1,4,5, 5,6,20,29,30}.

Fourth, the code shows

public  class JavaSort {
     public  static  void main (String [] args) {
         int A [] = new new  int [] {1,5,4,30,6,29,5,20 }; 
        System.out.println ( "Sort before the array: "+ of Arrays.toString (a)); 
        shellSort (a); 
        
        System.out.println ( " sorted array: "+ of Arrays.toString (a)); 
    } 
    / ** 
     * 
     * @param ary array to be sorted 
     * @return return the sorted array
      * / 
     public  static  int [] shellSort ( int [] ary) {
         int len = ary.length;
          for ( int NUM = len / 2; NUM> =. 1; NUM = NUM / 2) { // is the number of sequences num, num said phase difference sequence elements in the original sequence position
             / / find sequences, sequences with the insertion sort to sort 
             for ( int I = NUM; I <ary.length; I ++ ) {
                 int J = I-NUM; // sequence position of the elements to be inserted 
                int TEMP = ary [I ]; // sequence to be inserted element values staging 
                the while (J> 0 && TEMP <ary [J]) { // the elements to be inserted sequence element Comparative find the location to insert the element 
                    ary [J + NUM] = ary [J ]; 
                    J = J- NUM; 
                }
                ary [J  + NUM] = TEMP;// inserted sequence element to be inserted 
                
            } 
        } 
        return ary; 
         
     } 
}

operation result:

 

Guess you like

Origin www.cnblogs.com/shareAndStudy/p/12455186.html