Heap Sort (JAVA)

Source: Internet
Author: User

package org.rev.algorithm;/** *  Heap ordering, time complexity is O (Nlogn), is a sort of selection using the nature of the heap.  *  *  The Big Top heap is a fully binary tree, all the parent nodes are greater than or equal to its left and right child nodes, or a[i]>=a[2i+1]&&a[i]>=a[2i+2].   * (small top heap is parent node <= child node)  *  *  for full binary tree, the index value of the parent node of any node A[i] is (i-1)  / 2  down rounding.  *  * 1. For sequence a[0]-a[n-1], build a large top heap, heap top a[0] is the maximum value.  *  * 2.  Exchange Heap Top a[0] and the last element a[n-1], at this time, A[0]-a[n-2] is unordered, a[n-1] is ordered.  *  * 3.  A[0] may be against the big top heap, need to build the big top heap again, and exchange a[0] and a[n-2].  *     at this time a[0]-a[n-3] is unordered, a[n-2]-a[n-1] is orderly.  *  * 4.  so cyclic n times, I cycle, a[n-i]-a[n-1] are orderly, until i=n, at this time a[0]-a[n-1] is an ordered series.  *  */public class heapsort {  public static void main ( String[] args)  {    int[] data = {39, 11, 38, 97,  86, 37, 12, 4, 51, 18};    //  Heap Sort   &Nbsp; heapsort hs = new heapsort ();     hs.heapsort (data);     system.out.println ("After sorting:");     hs.print (data);  }   /**   *  Heap Sort    */  public void heapsort (int[] data)  {    for  (int i = 0; i < data.length; i++ )  { //  cyclic reactor       buildmaxheap (data, data.length -  1 - i); //  Build the heap, the sort result is ascending       // buildminheap (data,  Data.length - 1 - i); //  Build the heap, the sort result is descending       swap (data ,  0, data.length - 1 - i); //  Exchange heap top and last element        print (data);    }  }  /**   *  array data[ 0] to Data[lastindex]Building a large top heap    *    *  @param  data  waiting to build an array of    *   @param  lastIndex  index value of the last element in the array    */  public void  Buildmaxheap (Int[] data, int lastindex)  {    for  (int i  =  (lastindex - 1)  / 2; i >= 0; i--)  { //  Start with the parent node of the last element       int k = i; //  save the node currently being judged        while  (2 * k + 1 <= lastindex)  {  //  If the left child node of node Data[k] exists          //  Tmpindex records the index value of a larger node, and the initial value is the index value of the left child node of the current node         int tmpindex =  2 * k + 1;         //  Compare the left and right sub-nodes which big         //  meet this articleThe right child node is present, otherwise tmpindex should be equal to  lastIndex        if  (tmpindex  < lastindex)  {           //  ( DATA[2*K+1]&NBSP;&LT;DATA[2*K+2]), the left child node is less than the right child node           if   (data[tmpindex] < data[tmpindex + 1])  {              // tmpindex always records the index value of a larger node              tmpindex++;          }         }        //  Make maximum value at parent node         if  (Data[k] < data[tmpindex])   {          swap (Data, k, tmpindex); //  Swaps the position of two elements in an array           k = tmpindex;         } else {          break;         }      }    }  }   /**   *  Log Group data[0] to Data[lastindex] Build small top heap    *     *  @param  data  waiting to build an array of small top heaps    *  @param  lastIndex  The index value of the last element in the array    */  public void buildminheap (Int[] data, int lastindex)  {    for  (int i =  (lastindex - 1)  / 2;  i >= 0; i--)  { //  starts from the parent of the last element       int k  = i; //  Save the node currently being judged       while  (2 * k +  1 <= lAstindex)  { //  If the child node of the current node data[2k+1] exists         //  Tmpindex, the index value of the smaller node is recorded, and the initial value is the index value of the left child node of the current node         int tmpindex =  2 * k + 1;         //  compare left and right child nodes which small         //  satisfies this condition stating that the right child node is present, otherwise tmpindex should be equal to  lastIndex         if  (Tmpindex < lastindex)  {            //  (data[2*k+1] >data[2*k+2]), left child node greater than right child node            if  (Data[tmpindex] > data[tmpindex &NBSP;+&NBSP;1])  {             //  Tmpindex always records index values for smaller nodes             tmpIndex++;           }        }         //  make a small value at the parent node         if  (data[k] >  data[tmpindex])  {          swap (data, k,  tmpindex); //  swap the position of two elements in the array           k  = tmpindex;        } else {           break;        }       }    }  }  /**   *  swap the position of two elements in an array    *    *  @param  data   *  @param  i    *  @param  j   */  public void swap (Int[] data,  int i, int&nBSP;J)  {    if  (i == j)  {       Return;    }    /*     * int tmp  = data[i];      * data[i] = data[j];       * data[j] = tmp;     *       *  the following three lines of code and the above comment off the three sentence effect, the time for space-changing game.      */    data[i] = data[i] + data[j];     data[j] = data[i] - data[j];    data[i] =  data[i] - data[j];  }  /*   *  the elements in the output array    */   private void print (Int[] data)  {    for  (int i  = 0; i < data.length; i++)  {   &nbsP;  system.out.print (data[i] +  "\ T");    }     System.out.println ();   }}

Heap sorting method is not worth advocating for files with fewer records, but it is still very effective for n larger files. Because its run time is mainly spent on the initial heap under construction and the adjustment of the new heap when the repeated "filtering".

Heap sequencing in the worst case, the time complexity is also O (NLOGN). This is the greatest advantage of heap sorting relative to fast sorting. In addition, heap ordering requires only one record-size secondary storage space for Exchange.

Reference http://www.cnblogs.com/mengdd/archive/2012/11/30/2796845.html

Heap 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.