Heap Sort (heap sorting)

Source: Internet
Author: User

Heap Sort is common in written exams.

 

First of all, what is heap? Heap is a kind of data struct that can be seen as a Complete Binary Tree. the object to indicate heap is an array a that have two attributes: length [a] shows the number of elements in the array and heap-size [a] is the number of heap elements in the array. it means, though we have value in a [1 .. length [a], but the element after a [heap-size [a] do not belong to the heap, So heap-size [a] <= length [A]. the root of the heap (tree) is a [1], and giving any element I we can easily caculate its patent node, left child node and right child node, whichi is:

Parent (I)

Return I/2;

Left (I)

Return 2I;

Right (I)

Return 2I + 1;

There are two kind of heap: Max-heap and Min-heap. in max-heap, for every element I have CT the root shoshould obey the rule that the node is no bigger than its parent node, which means:

A [Parent (I)]> = A [I]

And in Min-heap, on the contrary, the node I is no smaller than its parent's:

A [Parent (I)] <= A [I]

In heap-sort, we use the concept of max-heap and the Min-heap can be used in construction of priority queue.

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.