using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsolePractice
{
public class Node
{
public int data;
public Node(int key)
{
data = key;
}
} class Heap
{
Node[] heapArray = null;
private int maxSize = ;
private int currSize = ;
public Heap(int maxSize)
{
this.maxSize = maxSize;
heapArray = new Node[maxSize];
} public bool InsertAt(int pos, Node nd)
{
heapArray[pos] = nd;
return true;
} public void ShowArray()
{
for (int i = ; i < maxSize; i++)
{
if (heapArray[i] != null)
Console.Write(heapArray[i].data + " ");
}
Console.WriteLine();
} public void ShiftUp(int index)
{
int parent = (index - ) / ;
Node bottom = heapArray[index];
while ((index > ) && (heapArray[parent].data < bottom.data))
{
heapArray[index] = heapArray[parent];
index = parent;
parent = (parent - ) / ;
}
heapArray[index] = bottom;
} public bool Insert(int key)
{
if (currSize == maxSize)
return false;
heapArray[currSize] = new Node(key);
currSize++;
return true;
} public Node Remove()
{
Node root = heapArray[];
currSize--;
heapArray[] = heapArray[currSize];
ShiftDown();
return root;
} public void ShiftDown(int index)
{
int largerChild;
Node top = heapArray[index];
while (index < (int)(currSize / ))
{
int leftChild = * index + ;
int rightChild = leftChild + ;
if ((rightChild < currSize) && heapArray[leftChild].data < heapArray[rightChild].data)
largerChild = rightChild;
else
largerChild = leftChild;
if (top.data >= heapArray[largerChild].data)
break;
heapArray[index] = heapArray[largerChild];
index = largerChild;
}
heapArray[index] = top;
}
} class C_shape
{
static void Main()
{
const int SIZE = ;
Heap aHeap = new Heap(SIZE);
Random RandomClass = new Random();
for (int i = ; i < SIZE; i++)
{
int rn = RandomClass.Next(, );
aHeap.Insert(rn);
} Console.WriteLine("Random:");
aHeap.ShowArray(); Console.WriteLine("Heap:");
for (int i = (int)SIZE / - ; i >= ; i--)
{
aHeap.ShiftDown(i);
}
aHeap.ShowArray(); Console.WriteLine("Sorted:");
for (int i = SIZE - ; i >= ; i--)
{
Node bigNode = aHeap.Remove();
aHeap.InsertAt(i, bigNode);
}
aHeap.ShowArray();
Console.ReadKey();
}
}
}

C#算法基础之堆排序的更多相关文章

  1. 数据结构和算法(Golang实现)(24)排序算法-优先队列及堆排序

    优先队列及堆排序 堆排序(Heap Sort)由威尔士-加拿大计算机科学家J. W. J. Williams在1964年发明,它利用了二叉堆(A binary heap)的性质实现了排序,并证明了二叉 ...

  2. Levenberg-Marquardt算法基础知识

    Levenberg-Marquardt算法基础知识 (2013-01-07 16:56:17) 转载▼   什么是最优化?Levenberg-Marquardt算法是最优化算法中的一种.最优化是寻找使 ...

  3. 解读Raft(一 算法基础)

    最近工作中讨论到了Raft协议相关的一些问题,正好之前读过多次Raft协议的那paper,所以趁着讨论做一次总结整理. 我会将Raft协议拆成四个部分去总结: 算法基础 选举和日志复制 安全性 节点变 ...

  4. 腾讯2017年暑期实习生编程题【算法基础-字符移位】(C++,Python)

     算法基础-字符移位 时间限制:1秒 空间限制:32768K 题目: 小Q最近遇到了一个难题:把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,且不能申请额外的空间. 你能帮帮小Q吗? ...

  5. 算法基础_递归_求杨辉三角第m行第n个数字

    问题描述: 算法基础_递归_求杨辉三角第m行第n个数字(m,n都从0开始) 解题源代码(这里打印出的是杨辉三角某一层的所有数字,没用大数,所以有上限,这里只写基本逻辑,要符合题意的话,把循环去掉就好) ...

  6. 毕业设计预习:SM3密码杂凑算法基础学习

    SM3密码杂凑算法基础学习 术语与定义 1 比特串bit string 由0和1组成的二进制数字序列. 2 大端big-endian 数据在内存中的一种表示格式,规定左边为高有效位,右边为低有效位.数 ...

  7. Python之算法基础

    1>递归相关: 递归:递归算法是一种直接或间接地调用自身算法的过程,在计算机编写程序中,递归算法对解决一大类问题是十分有效的,它往往使算法的描述简洁而且                   易于 ...

  8. Python 迭代器&生成器,装饰器,递归,算法基础:二分查找、二维数组转换,正则表达式,作业:计算器开发

    本节大纲 迭代器&生成器 装饰器  基本装饰器 多参数装饰器 递归 算法基础:二分查找.二维数组转换 正则表达式 常用模块学习 作业:计算器开发 实现加减乘除及拓号优先级解析 用户输入 1 - ...

  9. 算法基础:BFS和DFS的直观解释

    算法基础:BFS和DFS的直观解释 https://cuijiahua.com/blog/2018/01/alogrithm_10.html 一.前言 我们首次接触 BFS 和 DFS 时,应该是在数 ...

随机推荐

  1. HDU 5832 A water problem (水题,大数)

    题意:给定一个大数,问你取模73 和 137是不是都是0. 析:没什么可说的,先用char 存储下来,再一位一位的算就好了. 代码如下: #pragma comment(linker, "/ ...

  2. C++中void型指针

    问题由来: PX_FORCE_INLINE void* operator new(size_t size, const char* handle, const char * filename, int ...

  3. 根据powerdesigner的OO模型生成C#代码

    2007-05-15 08:34:11|  分类: 转贴部分 |  标签:学习帖子 |字号 订阅 习惯了用Powerdesigner设计数据库模型,XDE设计类图.因此我一般的设计方法是用PD做分析模 ...

  4. DX相机变换矩阵推导

    网上很多的推导过程都是错的,所以写一个. 先平移,再旋转就可以,先平移的原因是,如果先旋转的话,坐标系已经改了,所以先平移. 平移的变换和相机的变换是相反的,所以是: 平移完成后,相机的位置就和原点的 ...

  5. rxjava各种使用场景

    1. 数据的三级缓存 final Observable memory = Observable.create(new Observable.OnSubscribe() { @Override publ ...

  6. 通过SCVMM分配iSCSI存储

    除了使用基于SMB3.0应用程序的文件共享外,还可以使用iSCSI目标服务器的SAN存储,然后在SCVMM控制台中添加基于SMI-S类型的存储,步骤如下: 1.将一台安装了 iSCSI目标 功能的Wi ...

  7. BZOJ 1043 HAOI2008 下落的圆盘 计算几何

    题目大意:n个圆盘依次下落.求终于能看到的轮廓线面积 円盘反对! 让我们一起团结起来! 赶走円盘! 咳咳.非常神的一道题 今天去看了题解和白书才搞出来-- 首先我们倒着做 对于每一个圆盘处理出在它之后 ...

  8. Codeforces Beta Round #51 D. Beautiful numbers 数位dp

    D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...

  9. 获得临时文件目录(Temp文件夹)

    C:\Users\ADMINI~1\AppData\Local\Temp\   //GetTempPath获得临时文件目录(Temp文件夹)  function TempPath:String;var ...

  10. Mesos 配置项解析

    Mesos 的 配置项 能够通过启动时候传递參数或者配置文件夹下文件的方式给出(推荐方式,一目了然). 分为三种类型:通用项(master 和 slave 都支持).仅仅有 master 支持的,以及 ...