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. jq简单选项卡

    function tabControl(obj,elm){ $(obj).hover(function(){ $(this).addClass('active').siblings().removeC ...

  2. Java学习笔记(三):数组

    数组声明 java语言中,数组是一种最简单的复合数据类型.数组是有序数据的集合,数组中的每个元素具有相同的数据类型,可以用一个统一的数组名和下标来唯一地确定数组中的元素. int arr1[]; in ...

  3. 【转】Android WebRTC 音视频开发总结(一)

    http://www.cnblogs.com/lingyunhu/p/3578218.html 本系列文章主要总结和分享WebRTC开发过程中的一些经验,转载请说明出处(博客园RTC.Blacker) ...

  4. 2014广州Java岗位面试汇总

    本文记录了最近一些朋友提供的面试经历,真实数据,仅供广州求职的朋友参考.为行文方便,一律用主语”我“进行.部分词语可能造成读者不良反应,敬请留意. 1  广州沣首信息科技有限公司 公司所在区域相对较偏 ...

  5. 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 ...

  6. C语言经典算法100例(二)

    11.判断某一年是否是闰年. //判断某一年份是否是闰年 int IsLeapYear(int year) { return (year % 400 == 0 || (year % 4 == 0) & ...

  7. Java内部类的自我理解

    本文借鉴网络上多位大牛的博客和文章.感谢各位不知名人士的分享. 一.什么事内部类? 内部类是指在一个外部类的内部再定义一个类.内部类作为外部类的成员,而且依附于外部类而存在的.内部类能够为静态,可用p ...

  8. Online Schema Upgrade in MySQL Galera Cluster using TOI Method

    http://severalnines.com/blog/online-schema-upgrade-mysql-galera-cluster-using-toi-method     As a fo ...

  9. Ruby用法总结

    1.ruby中的整数.浮点数.字符串之间的相互转换 ruby的整数.浮点数.字符串的类均提供了to_i,to_f,to_s三个方法,分别用于转换成整数.转换成浮点数.转换成字符串. 2.数组的遍历 例 ...

  10. HOWTO install Oracle 11g on Ubuntu Linux 12.04 (Precise Pangolin) 64bits

    安装了Ubuntu 12.04 64bit, 想在上面安装Oracle 11gr2,网上找了好多文档都没成功,最后完全参考了MordicusEtCubitus的文章. 成功安装的关键点:install ...