The heap ordering of algorithm three

Source: Internet
Author: User

(heap) definition

(1) n a keyword sequence kl,k2,...,kn called (heap), when and only if the sequence satisfies the following properties (for short, heap properties):

K (i) <=k (2i) and K (i) <=k (2i+1) (1≤I≤N/2),

Of course, this is a small Gan, Dagen is replaced by >= number.

(2) k (i) is equivalent to a two-tree non-leaf node, K (2i) is the left Dial hand node, K (2i+1) is the right child node

If the vector stored in this sequence is R[1..N] as a storage structure of a complete binary tree, the heap is essentially a complete binary tree that satisfies the following properties:
The keywords of any non-leaf node in the tree are not greater than (or not less than) the keywords of their left and right child (if any) nodes.

Second, heap sort of thought heap sequencing using the Dagen (or small Gan) top record of the keyword maximum (or minimum) this feature, making it easy to select the maximum (or minimum) keywords in the current unordered region. (1) The basic idea of sorting with Dagen ① firstinitial file R[1..N] build a big pile, this heap is the initial unordered zone ② and then the keyword is the largestRecord R[1] (that is, the top of the heap) and the last record of the unordered zone R[n] Exchange, thus obtaining a new disorder region r[1..n-1] and an ordered area R[n], and satisfies R[1..n-1].keys≤r[n].key③ because of the exchange of new root r[1] may violate the nature of the heap, it should becurrent unordered area R[1..n-1] adjusted to heap。 Then again, the largest record of the r[1..n-1] r[1] and the last record of the interval r[n-1] exchange, resulting in a new unordered area R[1..n-2] and ordered area r[n-1. N], and still satisfies the relationship r[1..n-2].keys≤r[n-1. N].keys, R[1..n-2] will also be adjusted to the heap. ...... Until there is only one element in the unordered area.

Third, the implementation of the algorithm
    /*** Heap Sort *@paramData Queue*/     Public Static voidHeapsort (int[] data) {        //Initialize a large heap of         for(inti = DATA.LENGTH/2-1; I >= 0; i--) {adjustheap (data, I, data.length); //Specify parent node heap adjustment        }        intTemp//Temporary Space         for(inti = data.length-1; i > 0; i--) {            //stack head and end of stack Exchangetemp =Data[i]; Data[i]= Data[0]; data[0] =temp; //Large pile AdjustmentAdjustheap (data, 0, i); }    }    /*** Heap Adjustment *@paramData Queue *@paramStart start position *@paramend cutoff position, which is the next position of the end of the heap*/     Public Static voidAdjustheap (int[] Data,intStartintend) {                intSrc=data[start];//Save the starting position value         for(inti = start * 2 + 1; I < end; I *= 2 + 1) {            //determine the size of left and right nodes            if(I < end-1 && Data[i] < Data[i + 1]) {i++;//Large Right node            }            //Maximum starting position value            if(data[start]>Data[i]) Break; Data[start]=data[i];//maximum value of assignmentstart=i;//record the position of a large value} Data[start]=SRC;//Backfill Large Value position}

The time of the algorithm complexity heap sequencing is mainly composed of the time overhead of the two parts: the initial heap and the repeated reconstruction heap, which are all implemented by invoking Adjustheap.

Average performance O (N*LOGN), heap sorting is not appropriate for files with fewer records due to the number of comparisons required to build the initial heap.

The heap sort is an in-place sort and the secondary space is O (1). It is an unstable sort method. (The stability of a sort is that if there are two elements of the same order in the sorted sequence, their relative positions are not changed before and after sorting)

The heap ordering of algorithm three

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.