Skip to content

Commit d41f04d

Browse files
committed
2.4.41 Finished
1 parent 34f1187 commit d41f04d

File tree

9 files changed

+634
-0
lines changed

9 files changed

+634
-0
lines changed

2 Sorting/2.4/2.4.41/2.4.41.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>{39888A7A-52A1-4AB5-BCC9-599725C78D15}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>_2._4._41</RootNamespace>
10+
<AssemblyName>2.4.41</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.41/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.41/Program.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.IO;
3+
using PriorityQueue;
4+
5+
namespace _2._4._41
6+
{
7+
/*
8+
* 2.4.41
9+
*
10+
* Multiway 堆。
11+
* 根据正文中的描述实现基于完全堆有序的三叉树和四叉树的堆排序。
12+
* 对于 N=10^3、10^6 和 10^9 大小的随机不重复数组,
13+
* 记录你的程序所使用的比较次数和标准实现所使用的比较次数。
14+
*
15+
*/
16+
class Program
17+
{
18+
static Random random = new Random();
19+
20+
static void Main(string[] args)
21+
{
22+
Console.WriteLine("n\t2-way\t3-way\tRatio\t4-way\tRatio");
23+
24+
int n = 1000; // 当数据量到达 10^9 时会需要 2G 左右的内存
25+
int multiTen = 7;
26+
for (int i = 0; i < multiTen; i++)
27+
{
28+
Console.Write("10^" + (i + 3) + "\t");
29+
short[] data = GetRandomArray(n);
30+
BackupArray(data, i); // 暂存数组
31+
long originCount = HeapAnalysis.Sort(data);
32+
Console.Write(originCount + "\t");
33+
34+
RestoreArray(data, i); // 恢复数组
35+
long threeWayCount = HeapMultiwayAnalysis.Sort(data, 3);
36+
Console.Write(threeWayCount + "\t" + (float)threeWayCount / originCount + "\t");
37+
38+
RestoreArray(data, i); // 恢复数组
39+
long fourWayCount = HeapMultiwayAnalysis.Sort(data, 4);
40+
Console.WriteLine(fourWayCount + "\t" + (float)fourWayCount / originCount);
41+
42+
n *= 10;
43+
}
44+
}
45+
46+
static void BackupArray(short[] data, int index)
47+
{
48+
StreamWriter sw =
49+
File.CreateText
50+
(Environment.CurrentDirectory +
51+
Path.DirectorySeparatorChar +
52+
"data" + index + ".txt");
53+
for (int i = 0; i < data.Length; i++)
54+
{
55+
sw.WriteLine(data[i]);
56+
}
57+
sw.Flush();
58+
sw.Close();
59+
}
60+
61+
static void RestoreArray(short[] data, int index)
62+
{
63+
StreamReader sr =
64+
File.OpenText
65+
(Environment.CurrentDirectory +
66+
Path.DirectorySeparatorChar +
67+
"data" + index + ".txt");
68+
for (int i = 0; i < data.Length; i++)
69+
{
70+
data[i] = short.Parse(sr.ReadLine());
71+
}
72+
sr.Close();
73+
}
74+
75+
static short[] GetRandomArray(int n)
76+
{
77+
short[] data = new short[n];
78+
for (int i = 0; i < n; i++)
79+
{
80+
data[i] = (short)random.Next();
81+
}
82+
return data;
83+
}
84+
}
85+
}
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.41")]
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyProduct("2.4.41")]
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("39888a7a-52a1-4ab5-bcc9-599725c78d15")]
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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
3+
namespace PriorityQueue
4+
{
5+
/// <summary>
6+
/// d 叉堆排序类,提供堆排序的静态方法。
7+
/// </summary>
8+
/// <typeparam name="T">需要排序的元素类型。</typeparam>
9+
public static class HeapMultiway
10+
{
11+
/// <summary>
12+
/// 利用堆排序对数组进行排序。
13+
/// </summary>
14+
/// <param name="pq">需要排序的数组。</param>
15+
/// <param name="d">堆的分叉数。</param>
16+
public static void Sort<T>(T[] pq, int d) where T : IComparable<T>
17+
{
18+
int n = pq.Length;
19+
// 建堆
20+
for (int k = (n - 2) / d + 1; k >= 1; k--)
21+
{
22+
Sink(pq, k, n, d);
23+
}
24+
// 排序
25+
while (n > 1)
26+
{
27+
Exch(pq, 1, n--);
28+
Sink(pq, 1, n, d);
29+
}
30+
}
31+
32+
/// <summary>
33+
/// 令堆中的元素下沉。
34+
/// </summary>
35+
/// <param name="pq">需要执行操作的堆。</param>
36+
/// <param name="k">需要执行下沉的结点下标。</param>
37+
/// <param name="n">堆中元素的数目。</param>
38+
/// <param name="d">堆的分叉数。</param>
39+
private static void Sink<T>(T[] pq, int k, int n, int d) where T : IComparable<T>
40+
{
41+
while ((k - 1) * d + 2 <= n)
42+
{
43+
int j = d * (k - 1) + 2;
44+
// 在 d 个子结点中找到最大的那个
45+
for (int i = 0, q = j; i < d; i++)
46+
{
47+
if (q + i <= n && Less(pq, j, q + i))
48+
j = q + i;
49+
}
50+
if (!Less(pq, k, j))
51+
break;
52+
Exch(pq, k, j);
53+
k = j;
54+
}
55+
}
56+
57+
/// <summary>
58+
/// 比较堆中下标为 <paramref name="a"/> 的元素是否小于下标为 <paramref name="b"/> 的元素。
59+
/// </summary>
60+
/// <param name="pq">元素所在的数组。</param>
61+
/// <param name="a">需要比较是否较小的结点序号。</param>
62+
/// <param name="b">需要比较是否较大的结点序号。</param>
63+
/// <returns></returns>
64+
private static bool Less<T>(T[] pq, int a, int b) where T : IComparable<T> => pq[a - 1].CompareTo(pq[b - 1]) < 0;
65+
66+
/// <summary>
67+
/// 交换堆中的两个元素。
68+
/// </summary>
69+
/// <param name="pq">要交换的元素所在堆。</param>
70+
/// <param name="a">要交换的结点序号。</param>
71+
/// <param name="b">要交换的结点序号。</param>
72+
private static void Exch<T>(T[] pq, int a, int b)
73+
{
74+
T temp = pq[a - 1];
75+
pq[a - 1] = pq[b - 1];
76+
pq[b - 1] = temp;
77+
}
78+
}
79+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System;
2+
3+
namespace PriorityQueue
4+
{
5+
/// <summary>
6+
/// d 叉堆排序类,提供堆排序的静态方法。
7+
/// </summary>
8+
/// <typeparam name="T">需要排序的元素类型。</typeparam>
9+
public static class HeapMultiwayAnalysis
10+
{
11+
/// <summary>
12+
/// 堆排序的比较次数。
13+
/// </summary>
14+
private static long compareTimes;
15+
16+
/// <summary>
17+
/// 利用堆排序对数组进行排序,返回比较次数。
18+
/// </summary>
19+
/// <param name="pq">需要排序的数组。</param>
20+
/// <param name="d">堆的分叉数。</param>
21+
public static long Sort<T>(T[] pq, long d) where T : IComparable<T>
22+
{
23+
compareTimes = 0;
24+
long n = pq.Length;
25+
// 建堆
26+
for (long k = (n - 2) / d + 1; k >= 1; k--)
27+
{
28+
Sink(pq, k, n, d);
29+
}
30+
// 排序
31+
while (n > 1)
32+
{
33+
Exch(pq, 1, n--);
34+
Sink(pq, 1, n, d);
35+
}
36+
return compareTimes;
37+
}
38+
39+
/// <summary>
40+
/// 令堆中的元素下沉。
41+
/// </summary>
42+
/// <param name="pq">需要执行操作的堆。</param>
43+
/// <param name="k">需要执行下沉的结点下标。</param>
44+
/// <param name="n">堆中元素的数目。</param>
45+
/// <param name="d">堆的分叉数。</param>
46+
private static void Sink<T>(T[] pq, long k, long n, long d) where T : IComparable<T>
47+
{
48+
while ((k - 1) * d + 2 <= n)
49+
{
50+
long j = d * (k - 1) + 2;
51+
try
52+
{
53+
// 在 d 个子结点中找到最大的那个
54+
for (long i = 1, q = j; i < d; i++)
55+
{
56+
if (q + i <= n && Less(pq, j, q + i))
57+
j = q + i;
58+
}
59+
if (!Less(pq, k, j))
60+
break;
61+
}
62+
catch (Exception e)
63+
{
64+
Console.WriteLine("j=" + j);
65+
throw e;
66+
}
67+
68+
Exch(pq, k, j);
69+
k = j;
70+
}
71+
}
72+
73+
/// <summary>
74+
/// 比较堆中下标为 <paramref name="a"/> 的元素是否小于下标为 <paramref name="b"/> 的元素。
75+
/// </summary>
76+
/// <param name="pq">元素所在的数组。</param>
77+
/// <param name="a">需要比较是否较小的结点序号。</param>
78+
/// <param name="b">需要比较是否较大的结点序号。</param>
79+
/// <returns></returns>
80+
private static bool Less<T>(T[] pq, long a, long b) where T : IComparable<T>
81+
{
82+
compareTimes++;
83+
try
84+
{
85+
return pq[a - 1].CompareTo(pq[b - 1]) < 0;
86+
}
87+
catch (Exception e)
88+
{
89+
Console.WriteLine("a=" + a + "b=" + b);
90+
throw e;
91+
}
92+
}
93+
94+
/// <summary>
95+
/// 交换堆中的两个元素。
96+
/// </summary>
97+
/// <param name="pq">要交换的元素所在堆。</param>
98+
/// <param name="a">要交换的结点序号。</param>
99+
/// <param name="b">要交换的结点序号。</param>
100+
private static void Exch<T>(T[] pq, long a, long b)
101+
{
102+
T temp = pq[a - 1];
103+
pq[a - 1] = pq[b - 1];
104+
pq[b - 1] = temp;
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)