《github一天,一个算术题》:堆算法接口(堆排序、堆插入和堆垛机最大的价值,并删除)

《github一天,一个算术题》:堆算法接口(堆排序、堆插入和堆垛机最大的价值,并删除)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

阅览、认为、编写代码!

/*********************************************
 * copyright@hustyangju
 * blog: http://blog.csdn.net/hustyangju
 * 题目:堆排序实现,另外实现接口:取堆最大值并删除、堆插入
 * 思路:堆是在顺序数组原址上实现的。利用全然二叉树的性质。更具最大堆和最小堆的定义实现的。
 * 经典应用场景:内存中堆数据管理
 * 空间复杂度:堆排序是在原址上实现的,为0
 * 时间复杂度:堆排序为O(n lgn) ,取最值O(1)。插入最坏为O(lgn)
*********************************************/
#include <iostream>
#include <algorithm>

using namespace::std;

//对堆排序实现类的定义
class HeapSort
{
 public:
     HeapSort(int *pArray , int nArraySize);//constructor
     ~HeapSort();//destructor
 private:
    int *m_pA;//points to an array
    int m_nHeapSize;//stands for the size
 public:
    void BuildMaxHeap(); //build a heap
    void Sort();//建一个最大堆并排序。依照顺序(由小到大)放在原数组
    int  PopMaxHeap();//取最大堆的最大值
    void InsertMaxHeap(int a);//插入一个新值到最大堆,事实上就是在元素尾部增加一个值,再维护最大堆的性质
    void print();//顺序输出数组
 protected:
    int LeftChild(int node);//取左孩子下标
    int RightChild(int node);//取右孩子下标
    int Parent(int node);//取父节点下标
    void MaxHeapify(int nIndex);//justify the heap
 };

//构造函数初始化
HeapSort::HeapSort( int *pArray, int nArraySize )
{
     m_pA = pArray;
     m_nHeapSize = nArraySize;
}

//析构函数
HeapSort::~HeapSort()
{
}

//取左孩子下标。注意沿袭数组从0開始的习惯
int HeapSort::LeftChild(int node)
{
   return 2*node + 1;// the array starts from 0
}

//取右孩子下标
int HeapSort::RightChild(int node)
{
     return 2*node + 2;
}

//取父节点下标
int HeapSort::Parent(int node)
{
   return (node-1)/2 ;
}

//利用递归维护最大堆的性质。前提是已经建好最大堆。仅仅对变动的结点调用该函数
void HeapSort::MaxHeapify(int nIndex)
{
     int nLeft = LeftChild(nIndex);
     int nRight = RightChild(nIndex);

     int nLargest = nIndex;

     if( (nLeft < m_nHeapSize) && (m_pA[nLeft] > m_pA[nIndex]) )
         nLargest = nLeft;

     if( (nRight < m_nHeapSize) && (m_pA[nRight] > m_pA[nLargest]) )
        nLargest = nRight;

     if ( nLargest != nIndex )//假设有结点变动才继续递归
    {
         swap<int>(m_pA[nIndex], m_pA[nLargest]);
         MaxHeapify(nLargest);
     }
 }

//建造最大堆,思路:对于一个全然二叉树,子数组A[int((n-1)/2)+1]~A[n-1]为叶子结点
//A[0]~A[int((n-1)/2)]为非叶子结点。从下到上,从最后一个非叶子结点開始维护最大堆的性质
 void HeapSort::BuildMaxHeap()
 {
     if( m_pA == NULL )
         return;

     for( int i = (m_nHeapSize - 1)/2; i >= 0; i-- )
    {
         MaxHeapify(i);
     }
}

 //不断取最大堆的最大值A[0]与最后一个元素交换,将最大值放在数组后面。顺序排列数组
 void HeapSort::Sort()
{
     if( m_pA == NULL )
         return;
     if( m_nHeapSize == 0 )
        return;
    for( int i = m_nHeapSize - 1; i > 0; i-- )
     {
        swap<int>(m_pA[i], m_pA[0]);
         m_nHeapSize -= 1;//这个表达式具有破坏性!!

! MaxHeapify(0); }} //取出最大值,并在堆中删除 int HeapSort::PopMaxHeap() { /*if( m_pA == NULL ) return ; if( m_nHeapSize == 0 ) return ;*/ int a= m_pA[0]; m_pA[0]=m_pA[m_nHeapSize-1]; m_nHeapSize -= 1; MaxHeapify(0); return a; } //插入一个值。思路:放在数组最后面(符合数组插入常识),再逐层回溯维护最大堆的性质 void HeapSort::InsertMaxHeap(int a) { /* if( m_pA == NULL ) return; if( m_nHeapSize == 0 ) return; */ m_nHeapSize += 1; m_pA[m_nHeapSize-1]=a; int index=m_nHeapSize-1; while(index>0) { if(m_pA[index]>m_pA[Parent(index)]) { swap(m_pA[index], m_pA[Parent(index)]); index=Parent(index); } else index=0;//注意这里。某一层已经满足最大堆的性质了,就不须要再回溯了 } } //顺序输出数组 void HeapSort::print() { for(int i=0;i<m_nHeapSize;i++) cout<<m_pA[i]<<" "; cout<<endl; } int main() { int a[10]={6,5,9,8,1,0,3,2,7,4}; //int max; cout<<"input an array::"<<endl; for(int i=0;i<10;i++) cout<<a[i]<<" "; cout<<endl; HeapSort myHeap(a,10); myHeap.BuildMaxHeap(); cout<<"pop the max number:"<<endl; cout<<"the max="<<myHeap.PopMaxHeap()<<endl; cout<<"after pop:"<<endl; myHeap.print(); myHeap.InsertMaxHeap(11); cout<<"insert a number and sort:"<<endl; myHeap.Sort(); // myHeap.print(); for(int i=0;i<10;i++) cout<<a[i]<<" "; cout<<endl; }

測试结果:

《github一天,一个算术题》:堆算法接口(堆排序、堆插入和堆垛机最大的价值,并删除)

版权声明:本文博主原创文章。博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117131.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • [TensorFlowJS只如初见]实战三·使用TensorFlowJS拟合曲线

    [TensorFlowJS只如初见]实战三·使用TensorFlowJS拟合曲线问题描述拟合y=x*x-2x+3+0.1(-1到1的随机值)曲线给定x范围(0,3)问题分析在直线拟合博客中,我们使用最简单的y=wx+b的模型成功拟合了一条直线,现在我们在进一步进行曲线的拟合。简单的y=wx+b模型已经无法满足我们的需求,需要利用更多的神经元来解决问题了。代码&amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&amp;lt;script…

    2022年7月16日
    20
  • matlab读取txt文件为数组「建议收藏」

    matlab读取txt文件为数组「建议收藏」clc;clear;closeall;rows=[1180];%4行到17行。cols=[11];%3到8列。[FileName,PathName]=uigetfile(‘*.txt’,’SelecttheTxtfiles’);%弹出对话框,然后选择你要处理的文件fid=fopen([PathNameFileName]);temp=textscan(f…

    2025年9月18日
    8
  • 无尽的忙碌换来幸福的日子「建议收藏」

    人总是忙碌的,从小要读书,长大了工作,结婚了,有孩子了,一辈子也可能等到孩子成家了才能稍微休息一下下吧,不过有时候想想,忙碌点好,一辈子也就那么长,等闭了后还能休息好久好久呢,何不忙碌点呢。从过年以后,一直忙碌着,忙撒呢,上班忙新网站改版,下班忙结婚,周末也忙结婚,几乎一天都没有消停过,老婆无数次问我累不累,我说不累,再累也觉得幸福,嘿嘿。感叹了一下,好久也没来了,最近工作上呢刚赶出来一个…

    2022年4月13日
    34
  • LIS

    LIS

    2021年9月14日
    64
  • Please upgrade the installed version of powershell to the minimum required version and run the comma…

    Please upgrade the installed version of powershell to the minimum required version and run the comma…

    2021年10月28日
    46
  • springboot的自动化配置是什么_spring三种配置方式

    springboot的自动化配置是什么_spring三种配置方式一、什么是SpringBoot的自动配置?SpringBoot的最大的特点就是简化了各种xml配置内容,还记得曾经使用SSM框架时我们在spring-mybatis.xml配置了多少内容吗?数据源、连接池、会话工厂、事务管理···,而现在SpringBoot告诉你这些都不需要了,一切交给它的自动配置吧!所以现在能大概明白什么是SpringBoot的自动配置了吗?简单来说就是用注解来对一些常规的配置做默认配置,简化xml配置内容,使你的项目能够快速运行。是否对SpringBoot自动配置

    2022年8月22日
    10

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号