Skip to content

Commit 34f1187

Browse files
committed
2.4.40 Finished
1 parent 872bad4 commit 34f1187

File tree

9 files changed

+416
-2
lines changed

9 files changed

+416
-2
lines changed

2 Sorting/2.4/2.4.40/2.4.40.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>{CDF9928F-60D3-46B7-BCC3-170D3B2C6A8D}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>_2._4._40</RootNamespace>
10+
<AssemblyName>2.4.40</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.40/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.40/Program.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.IO;
3+
using PriorityQueue;
4+
5+
namespace _2._4._40
6+
{
7+
/*
8+
* 2.4.40
9+
*
10+
* Floyd 方法。
11+
* 根据正文中 Floyd 的先沉后浮思想实现堆排序。
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\tOrigin\tFloyd\tRatio");
23+
24+
int n = 1000; // 当数据量到达 10^9 时会需要 2G 左右的内存
25+
int multiTen = 7;
26+
for (int i = 0; i < multiTen; i++)
27+
{
28+
short[] data = GetRandomArray(n);
29+
BackupArray(data, i); // 暂存数组
30+
long originCount = HeapAnalysis.Sort(data);
31+
RestoreArray(data, i); // 恢复数组
32+
long floydCount = HeapFloydAnalysis.Sort(data);
33+
Console.WriteLine(n + "\t" + originCount + "\t" + floydCount + "\t" + (double)floydCount / originCount);
34+
n *= 10;
35+
}
36+
}
37+
38+
static void BackupArray(short[] data, int index)
39+
{
40+
StreamWriter sw =
41+
File.CreateText
42+
(Environment.CurrentDirectory +
43+
Path.DirectorySeparatorChar +
44+
"data" + index + ".txt");
45+
for (int i = 0; i < data.Length; i++)
46+
{
47+
sw.WriteLine(data[i]);
48+
}
49+
sw.Flush();
50+
sw.Close();
51+
}
52+
53+
static void RestoreArray(short[] data, int index)
54+
{
55+
StreamReader sr =
56+
File.OpenText
57+
(Environment.CurrentDirectory +
58+
Path.DirectorySeparatorChar +
59+
"data" + index + ".txt");
60+
for (int i = 0; i < data.Length; i++)
61+
{
62+
data[i] = short.Parse(sr.ReadLine());
63+
}
64+
sr.Close();
65+
}
66+
67+
static short[] GetRandomArray(int n)
68+
{
69+
short[] data = new short[n];
70+
for (int i = 0; i < n; i++)
71+
{
72+
data[i] = (short)random.Next();
73+
}
74+
return data;
75+
}
76+
}
77+
}
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.40")]
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyProduct("2.4.40")]
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("cdf9928f-60d3-46b7-bcc3-170d3b2c6a8d")]
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")]

2 Sorting/2.4/PriorityQueue/HeapAnalysis.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ public static class HeapAnalysis
1010
/// <summary>
1111
/// 堆排序的比较次数。
1212
/// </summary>
13-
private static int compareTimes;
13+
private static long compareTimes;
1414

1515
/// <summary>
1616
/// 利用堆排序对数组进行排序,返回比较次数。
1717
/// </summary>
1818
/// <param name="pq">需要排序的数组。</param>
19-
public static int Sort<T>(T[] pq) where T : IComparable<T>
19+
public static long Sort<T>(T[] pq) where T : IComparable<T>
2020
{
2121
compareTimes = 0;
2222
int n = pq.Length;
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
3+
namespace PriorityQueue
4+
{
5+
/// <summary>
6+
/// 堆排序类,提供 Floyd 优化的堆排序的静态方法。
7+
/// </summary>
8+
/// <typeparam name="T">需要排序的元素类型。</typeparam>
9+
public static class HeapFloyd
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+
// 建堆
19+
for (int k = n / 2; k >= 1; k--)
20+
{
21+
Sink(pq, k, n);
22+
}
23+
// 排序
24+
while (n > 1)
25+
{
26+
Exch(pq, 1, n--);
27+
SinkThenSwim(pq, 1, n);
28+
}
29+
}
30+
31+
/// <summary>
32+
/// 令堆中的元素下沉。
33+
/// </summary>
34+
/// <param name="pq">需要执行操作的堆。</param>
35+
/// <param name="k">需要执行下沉的结点下标。</param>
36+
/// <param name="n">堆中元素的数目。</param>
37+
private static void Sink<T>(T[] pq, int k, int n) where T : IComparable<T>
38+
{
39+
while (2 * k <= n)
40+
{
41+
int j = 2 * k;
42+
if (j < n && Less(pq, j, j + 1))
43+
j++;
44+
if (!Less(pq, k, j))
45+
break;
46+
Exch(pq, k, j);
47+
k = j;
48+
}
49+
}
50+
51+
/// <summary>
52+
/// 先下沉后上浮。
53+
/// </summary>
54+
/// <typeparam name="T">堆中的元素类型。</typeparam>
55+
/// <param name="pq">包含堆元素的数组。</param>
56+
/// <param name="k">要下沉的元素。</param>
57+
/// <param name="n">元素数量。</param>
58+
private static void SinkThenSwim<T>(T[] pq, int k, int n) where T : IComparable<T>
59+
{
60+
while (2 * k <= n)
61+
{
62+
int j = 2 * k;
63+
if (j < n && Less(pq, j, j + 1))
64+
j++;
65+
Exch(pq, k, j);
66+
k = j;
67+
}
68+
Swim(pq, k);
69+
}
70+
71+
/// <summary>
72+
/// 使元素上浮。
73+
/// </summary>
74+
/// <param name="k">需要上浮的元素。</param>
75+
private static void Swim<T>(T[] pq, int k) where T : IComparable<T>
76+
{
77+
while (k > 1 && Less(pq, k / 2, k))
78+
{
79+
Exch(pq, k, k / 2);
80+
k /= 2;
81+
}
82+
}
83+
84+
/// <summary>
85+
/// 比较堆中下标为 <paramref name="a"/> 的元素是否小于下标为 <paramref name="b"/> 的元素。
86+
/// </summary>
87+
/// <param name="pq">元素所在的数组。</param>
88+
/// <param name="a">需要比较是否较小的结点序号。</param>
89+
/// <param name="b">需要比较是否较大的结点序号。</param>
90+
/// <returns></returns>
91+
private static bool Less<T>(T[] pq, int a, int b) where T : IComparable<T> => pq[a - 1].CompareTo(pq[b - 1]) < 0;
92+
93+
/// <summary>
94+
/// 交换堆中的两个元素。
95+
/// </summary>
96+
/// <param name="pq">要交换的元素所在堆。</param>
97+
/// <param name="a">要交换的结点序号。</param>
98+
/// <param name="b">要交换的结点序号。</param>
99+
private static void Exch<T>(T[] pq, int a, int b)
100+
{
101+
T temp = pq[a - 1];
102+
pq[a - 1] = pq[b - 1];
103+
pq[b - 1] = temp;
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)