zoukankan      html  css  js  c++  java
  • 堆排序

    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)额外的空间;
           它是不稳定的;

  • 相关阅读:
    滑雪,不亦乐乎
    quant
    分享:Comment Remover 0003 发布
    shit 牛人要么出国了,要么在出国的路上
    linux目录跳转快捷方式——z武器
    迷你双核RK3066 安卓4.1智能网络高清TV 安卓播放器MK802III淘宝网
    分享:每天40分钟家务
    Marios Hadjieleftheriou/Frequent Items: Evaluation Source Code
    urllib2源码解读四(用opener打开你的url)
    分享:Editra 0.7.20 发布,跨平台文本编辑器
  • 原文地址:https://www.cnblogs.com/lucan727/p/3993058.html
Copyright © 2011-2022 走看看