zoukankan      html  css  js  c++  java
  • 数据结构之堆

    推排序:

    将完全二叉树构造成堆

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 构造堆
    {
        class Program
        {
            static void Main(string[] args)
            {
                //完全二叉树使用顺序存储的
                int[] array = new int[] { 34, 88, 56, 90, 85, 33, 24, 68, 94, 77, 57 };
                for (int i = array.Length/2; i >=1 ; i--)//对每个堆进行排序
                {
                    int MaxIndex = i;
                    int temp = i;
                    while(true)
                    {
                        int RightChild = temp * 2 + 1;
                        int LiftChild = temp* 2;
                        if(RightChild<=array.Length&&array[RightChild-1]>array[MaxIndex-1])//右节点大就把他放入父节点位置
                        {
                            MaxIndex = RightChild;
                        }
                        if(LiftChild<=array.Length&&array[LiftChild-1]>array[MaxIndex-1])//右节点大就把他放入父节点位置
                        {
                            MaxIndex = LiftChild;
                        }
                        if(MaxIndex!=temp)//如果节点调换位置,就把调换位置的节点在调节
                        {
                            int value = array[temp-1];
                            array[temp-1] = array[MaxIndex - 1];
                            array[MaxIndex - 1] = value;
                            temp = MaxIndex;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                foreach (var  item in  array)//将排序好的完全二叉树输出
                {
                    Console.WriteLine(item);
                }
                Console.ReadKey();
    
            }
        }
    }
  • 相关阅读:
    openmediavault 5.5.23 安装插件失败的解决方案
    qt下载地址
    qt 5.12 增加 mysql驱动
    选基金标准
    关注几个基金
    调仓的几个问题
    要读的书
    ubuntu 20.04 LTS 安装webmin
    set的常见用法
    斜率优化dp([HNOI2008]玩具装箱)
  • 原文地址:https://www.cnblogs.com/zhangyang4674/p/11325387.html
Copyright © 2011-2022 走看看