码迷,mamicode.com
首页 > 其他好文 > 详细

Heap Sort (堆排序)

时间:2014-07-26 00:13:06      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:style   strong   io   for   re   c   cti   ar   

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 expect the root should 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.

Heap Sort (堆排序),布布扣,bubuko.com

Heap Sort (堆排序)

标签:style   strong   io   for   re   c   cti   ar   

原文地址:http://www.cnblogs.com/yitong0768/p/3868310.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!