The Java implementation of Hill sort algorithm.

Source: Internet
Author: User
Package utils.sort;  /** * Hill sort, requires that the array to be sorted must implement the comparable interface * * * public class Shellsort implements Sortstrategy {    & nbsp;  private int[] increment;       /**        * Using hill Sort algorithm to sort by array obj         *        public void sort (comparable[] obj)         {              if (obj = = null)               {                      throw New NullPointerException ("The argument can not be null!");              }                  //Initialization step               initgap (obj);                  //Step Change (Descending)                for (int i = increment.length-1 i >= 0; i -)               {

                      int step = increment[i];                                          //Start by step position                       for (int j = Step; j < Obj.length; j+ +)                       {                             comparable TMP;                                                           //If the following is less than the previous (step by step), the exchange with the previous                              for (int m = j; M >= step; m = m-step)    &nbs p;                         {                                     if (Obj[m].compareto (Obj[m-step]) < 0)                                     {                                            tmp = Obj[m-step];                                             Obj[m-step] = obj[m];                                             obj[m] = tmp;                                    }                                     //Because the previous position must have been compared, so here's a direct exit loop                                      Else                                      {                                            break;                                    }                             }                      }              }        }           /**        * Determines the maximum exponent of an increment formula based on the length of the array, the formula is POW (4, I)-3 * POW (2, I) + 1 and 9 * POW (4, i)-9 * Pow 2, i) + 1      &nbs P * @return int[] The maximum exponent        * @param length array of two formulas         *        Private int[] initexponent (int length) &NBSP;&NBsp;     {              int[ ] Exp = new INT[2];               exp[0] = 1;               exp[1] =-1;               int[] gap = new INT[2];               gap[0] = gap[1] = 0;                              //Determine maximum index of two formulas                while (Gap[0] < length)                {                    &nBsp exp[0]++;                       gap[0] = (int) (Math.pow (4, exp[0])-3 * MATH.POW (2, exp[0]) + 1);                                      }                exp[0]--;                 while (gap[1] < length)               {                      exp[1]++;                       gap[1] = (int) (9 * MATH.POW (4, exp[1])-9 * Math. POW (2, exp[1]) + 1);                                       }               exp[1]--;               return exp;       }        private void Initgap (comparable[] obj)        {              //using the formula to initialize the increment sequence               int exp[] = Initexponent (obj.length);               int[] gap = new INT[2];  

               increment = new Int[exp[0] + exp[1]];                            //increment array from large to small assignment                for (int i = exp[0] + exp[1]-1; I >= 0; i--)      &nbs p;        {                      gap[0] = (int) (Math.pow (4, exp[0])-3 * MATH.POW (2, exp[0 ]) + 1);                        gap[1] = (int) (9 * MATH.POW (4, exp[1])-9 * MATH.POW (2, exp[1]) + 1);                       //Place large increments first into an incremental array, which is actually a merge sort                      //No need to consider gap[0] = = Gap[1] Situation because it is impossible to appear equal.                       if (gap[0] > gap[1])                       {                             Increment[i] = GAP[0];                              exp[0]--;                      } &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;             Else                       {                              Increment[i] = gap[1];                              exp[1]--;                      }              }                    }}  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.