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

堆排序

时间:2014-09-25 19:07:27      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:io   os   使用   ar   for   数据   sp   art   on   

Heap Sort:
Heapsort uses the property of Heaps to sort an array. The Heap data structure is an array object that can be viewed as a complete and balanced binary tree. Min (Max) Heap has a property that for every node other than the root, the value of the node is at least (at most) the value of its parent. Thus, the smallest (largest) element in a heap is stored at the root, and the subtrees rooted at a node contain larger (smaller) values than does the node itself.

堆排序 :
堆排序就是使用堆的属性数组进行排序。堆数据结构,可以被看作是一个完整的,平衡二叉树的数组对象。堆分大顶堆和小顶堆,小顶堆(大顶推)都有哟个属性:除了根以外的每一个节点,其值至少(至多)是该节点的父节点的值。由这个性质可知大顶堆的堆顶的值肯定是所有值中最大的,小顶堆的堆顶的值是所有值中最小的,因此,对中最小的(最大的)元素被存储在根目录,而且一个节点的子树包含一些比自己较大的(较小的)节点。

Algorithm:
It starts with building a heap by inserting the elements entered by the user, in its place.
1. Increase the size of the heap as a value is inserted.
2. Insert the entered value in the last place.
3. Compare the inserted value with its parent, until it satisfies the heap property and then place it at its right position.

算法:
它开始于 通过插入由用户输入的元素,在其位置上构建一个堆。
1,增加堆的大小作为一个插入的值
2,将输入值插在最后的地方。
3,插入值与其父值比较,直到满足堆属性,然后
将其放置在其正确位置。

1. heap[1] is the minimum element. So we remove heap[1]. Size of the heap is decreased.
2. Now heap[1] has to be filled. We put the last element in its place and see if it fits. If it does not fit, take minimum element among both its children and replaces parent with it.
3. Again see if the last element fits in that place.
Now once the heap is built remove the elements from top to bottom, while maintaining the heap property to obtain the sorted list of entered values.

1堆[1]是最小的元素。因此,我们删除堆[1]。堆的大小减小。
2,现在堆[1]已被填补。我们把最后一个元素,在它的地方,看看它是否适合。如果不适合,采取两子节点中的最小元素替代父节点。
3,再看看最后一个元素在那个地方适合。
现在首先堆从顶部移除元素到底部,同时保持堆属性来获取输入的值的排序列表。


Property:
Best case performance – when the input array is already sorted O(nlogn).
Worst case performance – when the input array is in reverse order O(nlogn).
Average case performance – O(nlogn)
It does not require any extra space for sorting, hence O(1) extra space.
It is not stable.


特性:
       最好情况下执行 - 当输入数组早已排序好O(nlogn);
       最坏情况下执行 - 当输入数组是倒序 O(nlogn);
       平均情况下执行 - O(nlogn);
       它不需要一个额外的空间来排序,因此 O(1)额外的空间;
       它是不稳定的;

堆排序

标签:io   os   使用   ar   for   数据   sp   art   on   

原文地址:http://www.cnblogs.com/lucan727/p/3993058.html

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