[Unity][Heap sort]用Unity动态演示堆排序的过程(How Heap Sort Works)
[Unity][Heap sort]用Unity动态演示堆排序的过程
How Heap Sort Works
最近做了一个用Unity3D动态演示堆排序过程的程序。
I've made this app to show how heap sort works recently.
效果图(Demo)
一图抵千言。
A picture paints a thousand words.
堆排序(Heap Sort)
堆排序总是建立这样一个二叉树:其父结点总大于其子结点。
Step 1: The first step of heap sort is to build a binary tree in which the parent node's value is always greater than its children nodes' value.
首先建堆。
It's called building the heap.
每轮将根结点与最后一个结点互换,然后对剩余部分建堆。
Step 2: Ater that, we swap the root node and the last node that hasn't been swapped yet.
Step 3: build the heap again like in step 1.
Step 4: if not all nodes are swapped in Step 2, goto Step2. Otherwise goto step 5.
Step 5: the heap sort is finished.
private static void HeapSortAscending1<T>(this IList<T> arr)
where T : IComparable
{
for (int i = arr.Count / - ; i >= ; i--)
{
arr.HeapAdjustAscending1(i, arr.Count);
}
for (int i = arr.Count - ; i > ; i--)
{
T temp = arr[];
arr[] = arr[i];
arr[i] = temp;
arr.HeapAdjustAscending1(, i);
}
}
private static void HeapAdjustAscending1<T>(this IList<T> arr, int nonLeafNodeToBeAdjusted, int unRangedCount)
where T:IComparable
{
int leftChild = nonLeafNodeToBeAdjusted * + ;
int rightChild = nonLeafNodeToBeAdjusted * + ;
int max = nonLeafNodeToBeAdjusted;
if (nonLeafNodeToBeAdjusted < unRangedCount / ) // 是非叶节点(None leaf node)
{
if (leftChild < unRangedCount && arr[leftChild].CompareTo(arr[max]) > )
{ max = leftChild; }
if (rightChild < unRangedCount && arr[rightChild].CompareTo(arr[max]) > )
{ max = rightChild; }
if (max!=nonLeafNodeToBeAdjusted)
{
T temp = arr[max];
arr[max] = arr[nonLeafNodeToBeAdjusted];
arr[nonLeafNodeToBeAdjusted] = temp;
arr.HeapAdjustAscending1(max, unRangedCount);
}
}
}
Heap sort
下载(Download)
如需APK安装包,请留言留下您的邮箱。如果需要源码,请捐赠100元并留下您的邮箱。
If you want the HowHeapSortWorks.apk, please leave your email address.
If you want the source code, please kindly donate ¥50 and leave your email adress:)
更新(Update)
2015-06-18
增加了'Random array'按钮,用于自动生成15个随机数,方便使用新case。
Added 'Random array' button which generates 15 random numbers as a new test case.
2015-06-21
在下方的数组显示索引。
display index for line nodes.
用表示堆所在层的颜色给下方的数组显示底色。
add background colors for line nodes.
[Unity][Heap sort]用Unity动态演示堆排序的过程(How Heap Sort Works)的更多相关文章
- PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...
- 选择排序、快速排序、归并排序、堆排序、快速排序实现及Sort()函数使用
1.问题来源 在刷题是遇到字符串相关问题中使用 strcmp()函数. 在函数比较过程中有使用 排序函数 Sort(beg,end,comp),其中comp这一项理解不是很彻底. #include & ...
- Unity编程标准导引-Unity中的基本概念-2.1界面概览
Unity中的基本概念 本文我们介绍Unity中的基本概念,包括:场景.游戏对象.组件.预制件.资源等. 2.1.界面概览 打开Unity之后,我们大概可以看到以上画面,以上画面中即显示了我们最常用到 ...
- Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作
刚进公司给安排的任务就是Unity接入高德地图,算是踩了不少坑总算做出来了,抽点时间写个博客记录一下 废话不多说 先上效果图 获取定位并根据手机朝向显示周边信息 使用的Unity ...
- unity中调试模型时unity崩溃问题
这个问题是在我调试3D模型资源时出现的,每当在Scene场景中调试模型时unity崩溃,出现Unity Bug Reporter页面,反复出现这个问题,很烧脑 对于这个问题我表示很无语,但是经过不断查 ...
- [IoC容器Unity]第一回:Unity预览
1.引言 高内聚,低耦合成为一个OO架构设计的一个参考标准.高内聚是一个模块或者一个类中成员跟这个模块或者类的关系尽量高,低耦合是不同模块或者不同类之间关系尽量简单. 拿咱国家举例来说,假如你是中国人 ...
- C#开发Unity游戏教程之Unity中方法的参数
C#开发Unity游戏教程之Unity中方法的参数 Unity的方法的参数 出现在脚本中的方法,无论是在定义的时候,还是使用的时候,后面都跟着一对括号“( )”,有意义吗?看起来最多也就是起个快速识别 ...
- 关于csdn博客中案例效果的动态演示
在曾经一篇博文中,网友评论说要是案例效果是动态演示的就好了,我认为说的非常是有道理.由于一个简单的截图不能非常好的展示案例效果.要是有一张gif图能动态的播放案例效果就再好只是了.在这里提供一个小软件 ...
- 动态演示冒泡排序java
动态演示冒泡排序java //冒泡排序是一种简单的交换排序,基本思路,从数列左边开始扫描元素,在扫描过程中依次对相邻元素进行比较,将较大元素后移. public class NumberSort { ...
随机推荐
- Git 简介
版本控制 什么是版本控制? 我需要版本控制吗? - 如果你还没使用过版本控制系统,或许你会有以上疑问,甚至更多疑问.希望后面的回答能让你喜欢上版本控制系统,喜欢上Git. 什么是版本控制:顾名思义,版 ...
- 调用WCF不需要添加服务引用,使用一个WCFHelper类就可以
效果图: 调用过程: string WCFURL = "http://localhost:100/Service1.svc"; UserRequest user = new Use ...
- 窗体Showmedol 遇到的奇怪异常-->进阶问题
procedure SetTransparentForm (popupFrm:TForm;Color:TColor;AlphaBlendValue:Integer); var FrmTranspare ...
- Go语言程序设计(1)--基本语法
第一个程序 package main import "fmt" func main() { fmt.Printf("Hello world") } 通过阅读这个 ...
- 压力测试相关之ab命令
1. 短时压力测试工具 ab 命令(apache的工具) 关键指标: Requests per second: 98.52 [#/sec] (mean) ###平均每秒的请求数 Tim ...
- files list file for package 'xxx' is missing final newline
#!/usr/bin/python # 8th November, 2009 # update manager failed, giving me the error: # 'files list f ...
- React-webpack开发需要的那些坑
nvariant Violation: _registerComponent(...): Target container is not a DOM element. 就是一个找不到DOM节点的问题, ...
- liunux 修改hostname
最近鼓捣Oracle,记录些技巧 修改hostname # vim /ect/hosts # vim /etc/sysconfig/network 修改hostname # service netwo ...
- requirejs 打包 添加版本号收集资料 待测试
https://www.npmjs.org/package/rjs-optimhttps://www.npmjs.org/package/grunt-requirejs-md5指定js版本号 但不是M ...
- 如何在Windows上从源码编译Chromium (CEF3) 加入mp3支持
一.什么是CEF CEF即Chromium Embeded Framework,由谷歌的开源浏览器项目Chromium扩展而来,可方便地嵌入其它程序中以得到浏览器功能. CEF包括CEF1和CEF3两 ...