Java Heap Sort Implementation

Source: Internet
Author: User

The code is as follows:

 Public classHeapsort { Public Static voidheapsort (datawrap[] data) {SYSTEM.OUT.PRINTLN ("Start sorting"); intLength =data.length; //Loop Build Heap         for(inti = 0;i < length-1; i++)        {            //Build a heapBuiilmaxdheap (data, length-1-i); //swap heaps and the last elementSwap (data, 0, length-1-i);        System.out.println (java.util.Arrays.toString (data)); }    }    //exchanging elements at index I and J two in the data array    Private Static voidSwap (datawrap[] data,intIintj) {Datawrap tmp=Data[i]; Data[i]=Data[j]; DATA[J]=tmp; }    //build the largest heap from 0 to lastindex for the data array    Private Static voidBuiilmaxdheap (datawrap[] data,intlastindex) {        //starting at the parent node of the lastindex node (the last node)         for(inti = (lastindex-1)/2;i >= 0;i--)        {            //K Save the node currently being judged            intK =i; //If the child node of the current K-node exists             while(k * 2+1 <=lastindex) {                //index of the left child node of the K-node                intBiggerindex = 2*k+1; //if Biggerindex is less than lastindex, that is biggerindex+1                if(Biggerindex <lastindex) {                    //if the value of the right child node is large                    if(Data[biggerindex].compareto (data[biggerindex+1]) < 0)                    {                        //Biggerindex always records the index of a larger child nodebiggerindex++; }                }                //if the value of the K node is less than the value of the larger byte point                if(Data[k].compareto (Data[biggerindex]) < 0)                {                    //swap themSwap (data, k, biggerindex); //assigns the Biggerindex to K, starting the next loop of the while loopK =Biggerindex; }                Else                {                     Break; }            }        }    }     Public Static voidMain (string[] args) {Datawrap [] data= {                NewDatawrap (21, ""),                NewDatawrap (30, ""),                NewDatawrap (49, ""),                NewDatawrap (30, ""),                NewDatawrap (21, ""),                NewDatawrap (16, ""),                NewDatawrap (9, "")        }; System.out.println ("Before sorting: \ n" +java.util.Arrays.toString (data));        Heapsort (data); System.out.println ("After sorting: \ n" +java.util.Arrays.toString (data)); }}

Description

The key to the heap sequencing above is the Buildmaxheap () method. This method is used to build a large top heap on the data array from the 0 to the lastindex indexed range, so that the largest element in the range of 0 to Lastindex is selected. The heap sequencing can be done by repeating the above process in a loop.

For the heap sorting algorithm, the pseudo-set of n data, the need to carry out a n-1 heap, each time the heap itself time is log2n, then its time efficiency is O (nlog2n). Heap sorting is highly efficient, requiring only an additional program unit for Exchange, with an O (1) space efficiency.

Java Heap Sort Implementation

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.