[Unity][Heap sort]用Unity动态演示堆排序的过程

[Unity][Heap sort]用Unity动态演示堆排序的过程

How Heap Sort Works

最近做了一个用Unity3D动态演示堆排序过程的程序。

I‘ve made this heap sort demo to show how heap sort works recently.

效果图(Demo)

一图抵千言。

A picture paints a thousand words.

您可以在此查看完整的动态GIF效果图。博客园里放不下这么大的GIF图。

链接:http://pan.baidu.com/s/1kT051pd 密码:zpy3

You can check out the whole gif here. THe blog don‘t support a big gif like it.

Link:http://pan.baidu.com/s/1kT051pd password:zpy3

堆排序(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.

 1         private static void HeapSortAscending1<T>(this IList<T> arr)
 2             where T : IComparable
 3         {
 4             for (int i = arr.Count / 2 - 1; i >= 0; i--)
 5             {
 6                 arr.HeapAdjustAscending1(i, arr.Count);
 7             }
 8             for (int i = arr.Count - 1; i > 0; i--)
 9             {
10                 T temp = arr[0];
11                 arr[0] = arr[i];
12                 arr[i] = temp;
13                 arr.HeapAdjustAscending1(0, i);
14             }
15         }
16         private static void HeapAdjustAscending1<T>(this IList<T> arr, int nonLeafNodeToBeAdjusted, int unRangedCount)
17             where T:IComparable
18         {
19             int leftChild = nonLeafNodeToBeAdjusted * 2 + 1;
20             int rightChild = nonLeafNodeToBeAdjusted * 2 + 2;
21             int max = nonLeafNodeToBeAdjusted;
22             if (nonLeafNodeToBeAdjusted < unRangedCount / 2) // 是非叶节点
23             {
24                 if (leftChild < unRangedCount && arr[leftChild].CompareTo(arr[max]) > 0)
25                 { max = leftChild; }
26                 if (rightChild < unRangedCount && arr[rightChild].CompareTo(arr[max]) > 0)
27                 { max = rightChild; }
28                 if (max!=nonLeafNodeToBeAdjusted)
29                 {
30                     T temp = arr[max];
31                     arr[max] = arr[nonLeafNodeToBeAdjusted];
32                     arr[nonLeafNodeToBeAdjusted] = temp;
33                     arr.HeapAdjustAscending1(max, unRangedCount);
34                 }
35             }
36         }

Heap sort

下载(Download)

您可以在此下载安卓APK。如果需要源码,请捐赠10元并留下您的邮箱。

You can download the HowHeapSortWorks.apk for android below here.

链接:http://pan.baidu.com/s/1c0GYIaO 密码:hlvi

Link:http://pan.baidu.com/s/1c0GYIaO password:hlvi

If you want the source code, please kindly donate ¥10 and leave your email adress:)

时间: 2024-08-03 22:13:19

[Unity][Heap sort]用Unity动态演示堆排序的过程的相关文章

PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)

题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是,后面是从小到大排列的最大的几个数p~n-1,前面第一位则是p-1. 所以只要先按照插入排序的特点来判断是否为插入排序,接下来再进行操作就可以了,这里要手动写下最大堆的更新操作. 代码: #include <iostream> #include <cstdio> #include <

algorithm: heap sort in python 算法导论 堆排序

An Python implementation of heap-sort based on the detailed algorithm description in Introduction to Algorithms Third Edition import random def max_heapify(arr, i, length): while True: l, r = i * 2 + 1, i * 2 + 2 largest = l if l < length and arr[l]

怎样动态演示椭圆的画法?

根据椭圆的定义,可以知道画椭圆的方法有很多,那么要怎么演示动态画椭圆呢?这就需要借助专业的画图软件几何画板了,下面就来学习具体绘制方法. 几何画板是一个优秀的专业学科平台软件,代表了当代专业工具平台类教学软件的发展方向.比如在学习椭圆圆锥曲线知识时,如果仅依靠黑板教学,是没法动态演示椭圆的,只能按照椭圆数据按部就班的绘图,但是如果利用几何画板,就可以动态演示画椭圆过程,而且还可以对椭圆的高度进行控制,下面就一起学习画动态画椭圆的方法. 几何画板软件免费获取地址:http://www.jihehu

堆排序(Heap Sort)的C语言实现

堆排序(Heap Sort)具体步骤为 将无序序列建成大顶堆(小顶堆):从最后一个非叶子节点开始通过堆调整HeapAdjust()变成小顶堆或大顶堆 将顶部元素与堆尾数组交换,此是末尾元素就是最大值,顶部元素不满足堆,故要将顶部元素在剩余的i-1个元素中调整为堆 反复第2步.直至所有顶点被输出,序列变成从小到大的有序序列 C语言实现(编译器Dev-c++5.4.0,源代码后缀.cpp) 原创文章,转载请注明来自钢铁侠Mac博客http://www.cnblogs.com/gangtiexia 1

堆排序 Heap Sort

堆排序是一种选择排序,其时间复杂度为O(nlogn). 堆的定义 n个元素的序列{k1,k2,…,kn}当且仅当满足下列关系之一时,称之为堆. 情形1:ki <= k2i 且ki <= k2i+1 (最小化堆或小顶堆) 情形2:ki >= k2i 且ki >= k2i+1 (最大化堆或大顶堆) 其中i=1,2,…,n/2向下取整; 若将和此序列对应的一维数组(即以一维数组作此序列的存储结构)看成是一个完全二叉树,则堆的含义表明,完全二叉树中所有非终端结点的值均不大于(或不小于)其左

Heap Sort (堆排序)

Heap sort is common in written exams. First of all, what is heap? Heap is a kind of data struct that can be seen as a complete binary tree. The object to indicate heap is an array A that have two attributes: length[A] shows the number of elements in

转载:堆排序 Heap Sort

堆排序是一种选择排序,其时间复杂度为O(nlogn). 堆的定义 n个元素的序列{k1,k2,…,kn}当且仅当满足下列关系之一时,称之为堆. 情形1:ki <= k2i 且ki <= k2i+1 (最小化堆或小顶堆) 情形2:ki >= k2i 且ki >= k2i+1 (最大化堆或大顶堆) 其中i=1,2,…,n/2向下取整; 若将和此序列对应的一维数组(即以一维数组作此序列的存储结构)看成是一个完全二叉树,则堆的含义表明,完全二叉树中所有非终端结点的值均不大于(或不小于)其左

数据结构 - 堆排序(heap sort) 详解 及 代码(C++)

堆排序(heap sort) 详解 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 堆排序包含两个步骤: 第一步: 是建立大顶堆(从大到小排序)或小顶堆(从小到大排序), 从下往上建立; 如建堆时, s是从大到小; 第二步: 是依次交换堆顶和堆底, 并把交换后的堆底输出, 只排列剩余的堆, 从上往下建立; 如构造时, s始终是1; 代码: /* * main.cpp * * Created on: 2014.6.12 * Author: S

算法分析-堆排序 Heap Sort

堆排序的是集合了插入排序的单数组操作,又有归并排序的时间复杂度,完美的结合了2者的优点. 堆的定义 n个元素的序列{k1,k2,…,kn}当且仅当满足下列关系之一时,称之为堆. 情形1:ki <= k2i 且ki <= k2i+1 (最小化堆或小顶堆) 情形2:ki >= k2i 且ki >= k2i+1 (最大化堆或大顶堆) 其中i=1,2,…,n/2向下取整; 若将和此序列对应的一维数组(即以一维数组作此序列的存储结构)看成是一个完全二叉树,则堆的含义表明,完全二叉树中所有非终