Heap sorting heap Sort

Source: Internet
Author: User

Heap sorting is called the heap sort, but it has no actual relationship with the heap in memory. On the algorithm, heap ordering is generally implemented in the form of arrays, that is, binary heap.

We can consider the heap int[] heap used by the heap sort as a complete binary tree, that is, unless there are empty nodes on the right side of the last layer, the others are full nodes.

For any heap[i] there are some properties:

1. I starting from 1.

2. Heap[i] 's parent node is HEAP[I/2].

3. Heap[i] The left child node is heap[i * 2], right child node is heap[i * 2 + 1].

We assume that this heap is the largest heap, that is, the parent node is larger than all the child nodes, and each subtree has the same nature. The following methods, which are related to heap sequencing, come from the idea of the introduction to algorithms.

Maxifyheap method, the node of index in the heap destroys the nature of the whole heap, but the position of the other nodes is right. We will first Heap[index] and its left and right sub-nodes, the larger of that swap position, such as the leave, and then recursively call Maxifyheap (left). Why is it assumed that only index is the wrong node? Because this method is usually called after the heap is built, that is, the fact that the heap is already the largest heap. The time complexity of this method is O (Logn).

Buildmaxheap method, build heap. That is, in the initial state of the heap, very chaotic, the first call. The idea is to recursively invoke the Maxifyheap method above, starting from the last node of the second level of the tree with nodes. Note that the starting node must be heaplength, and the end point is 1. The time complexity is O (n).

Heapsort method, first build the heap with Buildmaxheap, then the heap top element and the last element exchange position, this time the last element must be in place, Max. But the first element of the exchange must not be in the first place, notice that at this point the other nodes still do not destroy the maximum heap properties. So we just have to call Maxifyheap (1). So we, loop this, each time we swap the top and last elements of the heap for a position, Heaplength--。 Until Heaplength==1. The time complexity is O (NLOGN).

 Public classBinaryheap {Static intheaplength;  Public Static voidMaxifyheap (int[] Heap,intindex) {        if(Index >heaplength) {            return; }        intleft = index * 2, right = Index * 2 + 1; intlarger =-1; if(Left <=heaplength) {            if(Heap[left] >Heap[index]) {Larger=Left ; }        }        if(Right <=heaplength) {            if(Heap[right] > Heap[index] && heap[right] >Heap[left]) {Larger=Right ;        }} swap (heap, index, larger);    Maxifyheap (heap, larger); }     Public Static voidBuildmaxheap (int[] heap) {         for(inti = HEAPLENGTH/2; I >= 1; i--) {maxifyheap (heap, i); }    }     Public voidHeapsort (int[] heap)        {buildmaxheap (heap);  for(inti = heaplength; I >= 1; i--) {swap (heap,1, i); Heaplength--; Maxifyheap (Heap,1); }    }     Public Static voidSwapint[] Heap,intIndex1,intindex2) {        inttemp =HEAP[INDEX1]; HEAP[INDEX1]=Heap[index2]; HEAP[INDEX2]=temp; }}

Reference http://www.cnblogs.com/yangecnu/p/Introduce-Priority-Queue-And-Heap-Sort.html

Heap sorting heap Sort

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.