Skip to content

Commit 7dd8627

Browse files
committed
2.4.42 Finished
1 parent d41f04d commit 7dd8627

File tree

8 files changed

+560
-0
lines changed

8 files changed

+560
-0
lines changed

2 Sorting/2.4/2.4.42/2.4.42.csproj

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{BF782B32-E9C0-44B9-973E-AE7A402B0972}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>_2._4._42</RootNamespace>
10+
<AssemblyName>2.4.42</AssemblyName>
11+
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.Core" />
38+
<Reference Include="System.Xml.Linq" />
39+
<Reference Include="System.Data.DataSetExtensions" />
40+
<Reference Include="Microsoft.CSharp" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Net.Http" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="Program.cs" />
47+
<Compile Include="Properties\AssemblyInfo.cs" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<None Include="App.config" />
51+
</ItemGroup>
52+
<ItemGroup>
53+
<ProjectReference Include="..\PriorityQueue\PriorityQueue.csproj">
54+
<Project>{e040340e-ebb2-4650-8e19-ee46eb684e14}</Project>
55+
<Name>PriorityQueue</Name>
56+
</ProjectReference>
57+
</ItemGroup>
58+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
59+
</Project>

2 Sorting/2.4/2.4.42/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
5+
</startup>
6+
</configuration>

2 Sorting/2.4/2.4.42/Program.cs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
using System;
2+
using System.IO;
3+
using PriorityQueue;
4+
5+
namespace _2._4._42
6+
{
7+
/*
8+
* 2.4.42
9+
*
10+
* 堆的前序表示。
11+
* 用前序法而非级别表示一棵堆有序的树,
12+
* 并基于此实现堆排序。
13+
* 对于 N=10^3、10^6 和 10^9 大小的随机不重复数组,
14+
* 记录你的程序所使用的比较次数和标准实现所使用的比较次数。
15+
*
16+
*/
17+
class Program
18+
{
19+
static Random random = new Random();
20+
21+
static void Main(string[] args)
22+
{
23+
Console.WriteLine("n\torigin\tpre-order\tRatio");
24+
25+
int n = 1000; // 当数据量到达 10^9 时会需要 2G 左右的内存
26+
int multiTen = 7;
27+
for (int i = 0; i < multiTen; i++)
28+
{
29+
Console.Write("10^" + (i + 3) + "\t");
30+
short[] data = GetRandomArray(n);
31+
BackupArray(data, i); // 暂存数组
32+
long originCount = HeapAnalysis.Sort(data);
33+
Console.Write(originCount + "\t");
34+
35+
RestoreArray(data, i); // 恢复数组
36+
long preorderCount = HeapPreorderAnalysis.Sort(data);
37+
Console.WriteLine(preorderCount + "\t" + (float)preorderCount / originCount + "\t");
38+
n *= 10;
39+
}
40+
}
41+
42+
static bool IsSorted(short[] data)
43+
{
44+
for (int i = 0; i < data.Length - 1; i++)
45+
{
46+
if (data[i] > data[i + 1])
47+
return false;
48+
}
49+
return true;
50+
}
51+
52+
static void BackupArray(short[] data, int index)
53+
{
54+
StreamWriter sw =
55+
File.CreateText
56+
(Environment.CurrentDirectory +
57+
Path.DirectorySeparatorChar +
58+
"data" + index + ".txt");
59+
for (int i = 0; i < data.Length; i++)
60+
{
61+
sw.WriteLine(data[i]);
62+
}
63+
sw.Flush();
64+
sw.Close();
65+
}
66+
67+
static void RestoreArray(short[] data, int index)
68+
{
69+
StreamReader sr =
70+
File.OpenText
71+
(Environment.CurrentDirectory +
72+
Path.DirectorySeparatorChar +
73+
"data" + index + ".txt");
74+
for (int i = 0; i < data.Length; i++)
75+
{
76+
data[i] = short.Parse(sr.ReadLine());
77+
}
78+
sr.Close();
79+
}
80+
81+
static short[] GetRandomArray(int n)
82+
{
83+
short[] data = new short[n];
84+
for (int i = 0; i < n; i++)
85+
{
86+
data[i] = (short)random.Next();
87+
}
88+
return data;
89+
}
90+
91+
static void SplitTree(int[] data, int p, int n)
92+
{
93+
int k = (int)(Math.Log10(n) / Math.Log10(2)); // 高度
94+
95+
if (k == 0)
96+
return;
97+
Console.WriteLine("Spliting:");
98+
99+
Console.Write(data[p] + " | ");
100+
int left = (int)Math.Pow(2, k - 1) - 1;
101+
int right = left;
102+
if (n - left < (int)Math.Pow(2, k))
103+
{
104+
// 叶子结点全在左侧
105+
left = n - right - 1;
106+
}
107+
else
108+
{
109+
left = (int)Math.Pow(2, k) - 1;
110+
right = n - left - 1;
111+
}
112+
113+
// 输出
114+
int cursor = p + 1;
115+
for (int i = 0; i < left; i++)
116+
{
117+
Console.Write(data[cursor++] + " ");
118+
}
119+
Console.Write("| ");
120+
while (cursor < p + n)
121+
Console.Write(data[cursor++] + " ");
122+
Console.WriteLine();
123+
124+
SplitTree(data, p + 1, left);
125+
SplitTree(data, p + left + 1, right);
126+
}
127+
}
128+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Reflection;
2+
using System.Runtime.InteropServices;
3+
4+
// 有关程序集的一般信息由以下
5+
// 控制。更改这些特性值可修改
6+
// 与程序集关联的信息。
7+
[assembly: AssemblyTitle("2.4.42")]
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyProduct("2.4.42")]
12+
[assembly: AssemblyCopyright("Copyright © 2018")]
13+
[assembly: AssemblyTrademark("")]
14+
[assembly: AssemblyCulture("")]
15+
16+
// 将 ComVisible 设置为 false 会使此程序集中的类型
17+
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
18+
//请将此类型的 ComVisible 特性设置为 true。
19+
[assembly: ComVisible(false)]
20+
21+
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
22+
[assembly: Guid("bf782b32-e9c0-44b9-973e-ae7a402b0972")]
23+
24+
// 程序集的版本信息由下列四个值组成:
25+
//
26+
// 主版本
27+
// 次版本
28+
// 生成号
29+
// 修订号
30+
//
31+
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
32+
// 方法是按如下所示使用“*”: :
33+
// [assembly: AssemblyVersion("1.0.*")]
34+
[assembly: AssemblyVersion("1.0.0.0")]
35+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
using System;
2+
3+
namespace PriorityQueue
4+
{
5+
/// <summary>
6+
/// 前序堆排序类,提供堆排序的静态方法。
7+
/// </summary>
8+
/// <typeparam name="T">需要排序的元素类型。</typeparam>
9+
public static class HeapPreorder
10+
{
11+
/// <summary>
12+
/// 利用堆排序对数组进行排序。
13+
/// </summary>
14+
/// <param name="pq">需要排序的数组。</param>
15+
public static void Sort<T>(T[] pq) where T : IComparable<T>
16+
{
17+
int n = pq.Length;
18+
BuildTree(pq, 0, pq.Length);
19+
// 排序
20+
while (n > 1)
21+
{
22+
int tail = GetTail(pq, 0, n);
23+
T temp = pq[tail];
24+
for (int i = tail + 1; i < n; i++)
25+
pq[i - 1] = pq[i];
26+
n--;
27+
Exch(pq, 0, n);
28+
pq[0] = temp;
29+
Sink(pq, 0, n);
30+
}
31+
}
32+
33+
private static int GetTail<T>(T[] pq, int p, int n)
34+
{
35+
if (n <= 1)
36+
return p;
37+
int k = (int)(Math.Log10(n) / Math.Log10(2)); // 高度
38+
39+
int left = (int)Math.Pow(2, k - 1) - 1;
40+
int right = left;
41+
if (n - left <= (int)Math.Pow(2, k))
42+
{
43+
// 叶子结点全在左侧
44+
left = n - right - 1;
45+
return GetTail(pq, p + 1, left);
46+
}
47+
else
48+
{
49+
left = (int)Math.Pow(2, k) - 1;
50+
right = n - left - 1;
51+
return GetTail(pq, p + 1 + left, right);
52+
}
53+
}
54+
55+
/// <summary>
56+
/// 递归建堆。
57+
/// </summary>
58+
/// <typeparam name="T">堆中元素。</typeparam>
59+
/// <param name="pq">堆所在的数组。</param>
60+
/// <param name="p">堆的起始下标。</param>
61+
/// <param name="n">堆的元素数目。</param>
62+
private static void BuildTree<T>(T[] pq, int p, int n) where T : IComparable<T>
63+
{
64+
if (n <= 1)
65+
return;
66+
int k = (int)(Math.Log10(n) / Math.Log10(2)); // 高度
67+
68+
int left = (int)Math.Pow(2, k - 1) - 1;
69+
int right = left;
70+
if (n - left <= (int)Math.Pow(2, k))
71+
{
72+
// 叶子结点全在左侧
73+
left = n - right - 1;
74+
}
75+
else
76+
{
77+
left = (int)Math.Pow(2, k) - 1;
78+
right = n - left - 1;
79+
}
80+
81+
BuildTree(pq, p + 1, left);
82+
BuildTree(pq, p + 1 + left, right);
83+
Sink(pq, p, n);
84+
}
85+
86+
/// <summary>
87+
/// 令堆中的元素下沉。
88+
/// </summary>
89+
/// <param name="pq">需要执行操作的堆。</param>
90+
/// <param name="p">需要执行下沉的结点下标。</param>
91+
/// <param name="n">堆中元素的数目。</param>
92+
private static void Sink<T>(T[] pq, int p, int n) where T : IComparable<T>
93+
{
94+
if (n <= 1)
95+
return;
96+
int k = (int)(Math.Log10(n) / Math.Log10(2)); // 高度
97+
98+
int left = (int)Math.Pow(2, k - 1) - 1;
99+
int right = left;
100+
if (n - left <= (int)Math.Pow(2, k))
101+
{
102+
// 叶子结点全在左侧
103+
left = n - right - 1;
104+
}
105+
else
106+
{
107+
left = (int)Math.Pow(2, k) - 1;
108+
right = n - left - 1;
109+
}
110+
111+
// 找出较大的子结点
112+
int j = p + 1, size = left;
113+
if (right != 0) // 有右结点
114+
{
115+
if (Less(pq, j, p + left + 1))
116+
{
117+
j = p + left + 1;
118+
size = right;
119+
}
120+
}
121+
122+
// 与根结点比较
123+
if (!Less(pq, p, j))
124+
return;
125+
126+
// 交换,继续下沉
127+
Exch(pq, p, j);
128+
Sink(pq, j, size);
129+
}
130+
131+
/// <summary>
132+
/// 比较堆中下标为 <paramref name="a"/> 的元素是否小于下标为 <paramref name="b"/> 的元素。
133+
/// </summary>
134+
/// <param name="pq">元素所在的数组。</param>
135+
/// <param name="a">需要比较是否较小的结点序号。</param>
136+
/// <param name="b">需要比较是否较大的结点序号。</param>
137+
/// <returns></returns>
138+
private static bool Less<T>(T[] pq, int a, int b) where T : IComparable<T> => pq[a].CompareTo(pq[b]) < 0;
139+
140+
/// <summary>
141+
/// 交换堆中的两个元素。
142+
/// </summary>
143+
/// <param name="pq">要交换的元素所在堆。</param>
144+
/// <param name="a">要交换的结点序号。</param>
145+
/// <param name="b">要交换的结点序号。</param>
146+
private static void Exch<T>(T[] pq, int a, int b)
147+
{
148+
T temp = pq[a];
149+
pq[a] = pq[b];
150+
pq[b] = temp;
151+
}
152+
}
153+
}

0 commit comments

Comments
 (0)