Insert Sort (JAVA)

Source: Internet
Author: User

package org.rev.algorithm;/** *  Insert Sort: Each time a record to be sorted is inserted at its size into the appropriate position of the previously sorted subsequence until all is inserted.  *  * 1.  Direct Insert Sort: Compare the sorted parts to find the right location  *  * 2.  Binary insertion Sort: Use dichotomy to find the right position  *  * 3.  Hill sort (shell sort): Grouping of direct insert sort  *  */ Public class insertionsort {  public static void main (String[]  args)  {    int[] data = {11, 39, 38, 55, 86,  5, 37, 12, 4, 11, 18};    insertionsort is = new  insertionsort ();     is.directinsertionsort (data); //  Direct Insert Sort      // is.binaryinsertionsort (data); //  to insert sort     // is. Shellsort (data); //  Hill sort     is.print (data); //  output sorted array   }   /**   *  Direct insertion sequencing, complexity of Time complexity O (n^2)    *    * 1.  the 1th element is an ordered series of length 1, and the 2nd-nth element is the sequence to be sorted.    *    * 2.  starts with the 2nd element and compares the elements in the preceding ordinal sequence (from the back forward), finds the appropriate position, and inserts.    *    * 3.  until the nth element is inserted.    *    *  @param  data  series to sort    */   Public void directinsertionsort (Int[] data)  {    for  (int i  = 1; i < data.length; i++)  {      int  tmp = data[i];      int j;      &NBSP;//&NBSP;DATA[0]--DATA[J] is an ordered series       for  (j = i - 1 ;  j >= 0; j--)  {        if  (tmp  &LT;&NBSP;DATA[J])  { //  Most of the TMP           data[j + 1] = data[j];         } else {          break;         }      }      //   Put TMP in the front empty position.       data[j + 1] = tmp;    }   }  /**   *    *  binary insertion sequencing, time complexity O (n^2)     *    *  thinking and direct insertion, just find the right insertion position by using the binary search method, you can reduce the number of comparisons.    *    *  @param  data  series to sort    */   Public void binaryinsertionsort (Int[] data)  {    for  (int i  = 0; i < data.length; i++)  {      int  tmp = data[i];       // data[0] -- data[i-1] is an ordered series       int  left = 0;      int right = i - 1;       int mid = 0; //  Intermediate value, initially 0       //  find the location to be inserted by dichotomy, i.e. left      while  (left <= right)  {        mid =  (left + right)  / 2;         if  (Tmp < data[mid])  { //  If less than median           right = mid - 1;         } else { //  if the number is greater than or equal to the middle            left = mid + 1;         }      }      //  Everybody move back and move a little bit.        for  (int j = i - 1; j >= left; j--)  {         data[j + 1] = data[j];       }      //  into       data[ Sort of left] = tmp;    }  }  /**   *  Hill, is essentially a grouping insertion algorithm.    *    *  for series a1......an   *    *  1.  takes an integer d less than n as the first increment, divides the sequence into D-groups, shaped like a  {a[1],a[d+1],a[2d+1]),...}, {a[2],a[d+2],a[2d+2]),...}    *    * 2.  in each group, the direct insert sort, complete the first order.    *    * 3.  take the second increment D2 (d2 < d) and divide the sequence into d2  groups, Direct insert sorting is performed in each group, respectively.    *    * 4.  until increment equals 1.    *    *  @param  data  series to sort    */   Public void shellsort (Int[] data)  {    int d =  data.length;    while  (d > 1)  {       d = d / 2; //  takes an integer less than length       for  (int  x = 0; x < d; x++)  { //  divided into groups of D          //  in-group direct insertion Sort         for  (int i &NBSP;=&NBSP;X&NBSP;+&NBSP;D;&NBSP;I&NBSP;&LT;&NBSP;DATA.LENGTH;&NBSP;I&NBSP;=&NBSP;I&NBSP;+&NBSP;D)  {           int tmp = data[i];           int j;           for&nbSP; (j = i - d; j >= 0 && data[j] > tmp; &NBSP;J&NBSP;=&NBSP;J&NBSP;-&NBSP;D)  {             data[j + d] = data[j];          }           data[j + d] = tmp;         }      }    }   }  /*   *  print data from the output array    */  private void  print (Int[] data)  {    for  (int i = 0; i  < data.length; i++)  {      system.out.print (data[i] +   "\ t");     }    system.out.println ();   }}

Comparison of three kinds of insertion sorts:

1. Direct Insert Sort: stabilize the sort, insert the block when the sequence is relatively orderly than the two points.

2. Two points insertion sort: stable sorting, when n is large, sorting out unordered numbers is faster than inserting directly.

3. Hill sort: Improved direct insertion algorithm, fast, but not stable sort.



Insert Sort (JAVA)

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.