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. 解北大OJ1088滑雪问题的记录

    问题: Time Limit:1000MS   Memory Limit:65536K Total Submissions:67600   Accepted:24862 Description Mic ...

  2. [转]MyEclipse for Spring2014破解

    转至:http://blog.my-eclipse.cn/myeclipse-2014-crack.html 一.安装完成MyEclipse2014(适用于2013等版本)后,不要打开软件,下载破解附 ...

  3. hash_map vs unordered_map vs map vs unordered_set

    hash_map vs unordered_map 这两个的内部结构都是采用哈希表来实现.unordered_map在C++11的时候被引入标准库了,而hash_map没有,所以建议还是使用unord ...

  4. nm命令详解

    nm在linux中列出目标文件的符号清单,常用来查看动态链接库中的函数 nm支持的选项如下 -a   按照man手册,仅列出调试信息,实际上却是调试信息+正常信息 -A   增加一列显示目标文件,没有 ...

  5. I/O流的学习

    一.I/O流 1.判定是输入还是输出我们应该站在程序的立场: 2.判断传输的是字节还是字符,从而决定管道的大小,字节传递是根本,可以传递所有的数据类型,字符传递专门用来传递文本数据,字节主要用来传递二 ...

  6. Hibernate查询效率对比

    查询已知表名的实体时推荐使用getHibernateTemplate().executeWithNativeSession() + SQLQuery方式. 以下测试使用JUnit进行,仅查询一次,查询 ...

  7. zookeeper 系列

    ZooKeeper是Hadoop的正式子项目,它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护.名字服务.分布式同步.组服务等.ZooKeeper的目标就是封装好复杂易出错的关键服务 ...

  8. 在Linux下怎么确定哪个网卡对应哪个接口?

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...

  9. ios开发——常用经典算法OC篇&冒泡/快速

    冒泡排序与快速排序 1.序言 ios开发中涉及到算法的地方还真不多,除非你的应用程序真的非常大,或者你想你的应用程序性能非常好才会去想到关于算法方面的性能优化,而在ios开发中真的能用得到的也就是关于 ...

  10. UVA 1401 - Remember the Word(Trie+DP)

    UVA 1401 - Remember the Word [题目链接] 题意:给定一些单词.和一个长串.问这个长串拆分成已有单词,能拆分成几种方式 思路:Trie,先把单词建成Trie.然后进行dp. ...