基本信息
源码名称:C# 堆排序(.net core)
源码大小:0.19M
文件格式:.zip
开发语言:C#
更新时间:2020-07-28
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.ComponentModel.DataAnnotations;
namespace 堆排序
{
class Program
{
static void Main()
{
int[] tree = { 18 ,54 , 14 , 45 , 96 ,27, 80 , 22 , 81, 31,
0 , 63 , 49, 5 , 59 , 27, 28 , 75, 68, 52,
52 , 66 , 5 , 98, 44 ,19, 22 , 16 , 88, 34,
59 , 81, 12 , 85, 49, 54 , 84, 13 , 46 , 83,
88, 1 , 70 , 32 , 45, 32 , 38 , 41 , 4, 50};
heapsort(tree);
Console.WriteLine("排序后的数列:");
foreach (int n in tree)
Console.WriteLine(n "");
}
static void heapify(int[] tree, int i, int large)
{
int c1 = (2 * i) 1;
int c2 = (2 * i) 2;
int max = i;
if (c1 < large && tree[c1] > tree[max])
{
max = c1;
}
if (c2 < large && tree[c2] > tree[max])
{
max = c2;
}
if (i != max)
{
int Temp = tree[i];
tree[i] = tree[max];
tree[max] = Temp;
heapify(tree, max, large);
}
}
static void buildheap(int[] tree)
{
for (int j = (tree.Length / 2) - 1; j >= 0; j--)
{
heapify(tree, j, tree.Length);
}
}
static void heapsort(int[] tree)
{
buildheap(tree);
int i;
for (i = tree.Length - 1; i > 0; i--)
{
int Temp = tree[0];
tree[0] = tree[i];
tree[i] = Temp;
heapify(tree, 0, i);
}
}
}
}