Skip to content

Commit 47ac2de

Browse files
committed
#403, 2.5.33 Finished
1 parent e969198 commit 47ac2de

File tree

12 files changed

+491
-10
lines changed

12 files changed

+491
-10
lines changed

2 Sorting/2.5/2.5.33/2.5.33.csproj

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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>{71E47859-80B4-4C75-B1FC-2C022B7E13E9}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>_2._5._33</RootNamespace>
10+
<AssemblyName>2.5.33</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+
<Compile Include="TransactionGenerator.cs" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<None Include="App.config" />
52+
</ItemGroup>
53+
<ItemGroup>
54+
<ProjectReference Include="..\SortApplication\SortApplication.csproj">
55+
<Project>{121d6b05-a47f-4c1c-bad8-ee4e5a245c8e}</Project>
56+
<Name>SortApplication</Name>
57+
</ProjectReference>
58+
</ItemGroup>
59+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
60+
</Project>

2 Sorting/2.5/2.5.33/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.5/2.5.33/Program.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Diagnostics;
3+
using SortApplication;
4+
5+
namespace _2._5._33
6+
{
7+
/*
8+
* 2.5.33
9+
*
10+
* 随机交易。
11+
* 开发一个接受参数 N 的生成器,
12+
* 根据你能想到的任意假设条件生成 N 个随机的 Transaction 对象
13+
* (请见练习 2.1.21 和 2.1.22)。
14+
* 对于 N=10^3、10^4、10^5 和 10^6,
15+
* 比较用希尔排序、归并排序、快速排序和堆排序将 N 个交易排序的性能。
16+
*
17+
*/
18+
class Program
19+
{
20+
static void Main(string[] args)
21+
{
22+
Stopwatch stopwatch = new Stopwatch();
23+
ShellSort shellSort = new ShellSort();
24+
MergeSort mergeSort = new MergeSort();
25+
QuickSort quickSort = new QuickSort();
26+
27+
int n = 1000;
28+
int nMultipleBy10 = 4;
29+
for (int i = 0; i < nMultipleBy10; i++)
30+
{
31+
Console.WriteLine("n=" + n);
32+
Transaction[] trans = TransactionGenerator.Generate(n);
33+
34+
Transaction[] testCase = new Transaction[n];
35+
36+
trans.CopyTo(testCase, 0);
37+
stopwatch.Restart();
38+
shellSort.Sort(testCase);
39+
stopwatch.Stop();
40+
Console.WriteLine("Shell Sort: " + stopwatch.ElapsedMilliseconds + " ms");
41+
42+
trans.CopyTo(testCase, 0);
43+
stopwatch.Restart();
44+
mergeSort.Sort(testCase);
45+
stopwatch.Stop();
46+
Console.WriteLine("Merge Sort: " + stopwatch.ElapsedMilliseconds + " ms");
47+
48+
trans.CopyTo(testCase, 0);
49+
stopwatch.Restart();
50+
quickSort.Sort(testCase);
51+
stopwatch.Stop();
52+
Console.WriteLine("Quick Sort: " + stopwatch.ElapsedMilliseconds + " ms");
53+
54+
trans.CopyTo(testCase, 0);
55+
stopwatch.Restart();
56+
Heap.Sort(testCase);
57+
stopwatch.Stop();
58+
Console.WriteLine("Shell Sort: " + stopwatch.ElapsedMilliseconds + " ms");
59+
60+
n *= 10;
61+
}
62+
}
63+
}
64+
}
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.5.33")]
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyProduct("2.5.33")]
12+
[assembly: AssemblyCopyright("Copyright © 2019")]
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("71e47859-80b4-4c75-b1fc-2c022b7e13e9")]
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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Text;
3+
using SortApplication;
4+
5+
namespace _2._5._33
6+
{
7+
/// <summary>
8+
/// 随机交易生成器。
9+
/// </summary>
10+
class TransactionGenerator
11+
{
12+
private static Random random = new Random();
13+
14+
/// <summary>
15+
/// 生成 n 条随机交易记录。
16+
/// </summary>
17+
/// <param name="n">交易记录的数量。</param>
18+
/// <returns></returns>
19+
public static Transaction[] Generate(int n)
20+
{
21+
Transaction[] trans = new Transaction[n];
22+
for (int i = 0; i < n; i++)
23+
{
24+
trans[i] = new Transaction
25+
(GenerateName(),
26+
GenerateDate(),
27+
random.NextDouble() * 1000);
28+
}
29+
return trans;
30+
}
31+
32+
/// <summary>
33+
/// 获取随机姓名。
34+
/// </summary>
35+
/// <returns></returns>
36+
private static string GenerateName()
37+
{
38+
int nameLength = random.Next(4, 7);
39+
StringBuilder sb = new StringBuilder();
40+
41+
sb.Append(random.Next('A', 'Z' + 1));
42+
for (int i = 1; i < nameLength; i++)
43+
sb.Append(random.Next('a', 'z' + 1));
44+
45+
return sb.ToString();
46+
}
47+
48+
/// <summary>
49+
/// 获取随机日期。
50+
/// </summary>
51+
/// <returns></returns>
52+
private static Date GenerateDate()
53+
{
54+
int year = random.Next(2017, 2019);
55+
int month = random.Next(1, 13);
56+
int day;
57+
if (month == 2)
58+
day = random.Next(1, 29);
59+
else if ((month < 8 && month % 2 == 1) ||
60+
(month > 7 && month % 2 == 0))
61+
day = random.Next(1, 32);
62+
else
63+
day = random.Next(1, 31);
64+
65+
Date date = new Date(month, day, year);
66+
return date;
67+
}
68+
}
69+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
3+
namespace SortApplication
4+
{
5+
public class Date : IComparable<Date>
6+
{
7+
public int Month { get; } // 月
8+
public int Day { get; } // 日
9+
public int Year { get; } // 年
10+
11+
/// <summary>
12+
/// 构造函数。
13+
/// </summary>
14+
/// <param name="date">形如 "05/31/2017" 的字符串。</param>
15+
public Date(string date)
16+
{
17+
string[] a = date.Split('/');
18+
if (a.Length != 3)
19+
throw new ArgumentException("Illgal Date");
20+
this.Month = int.Parse(a[0]);
21+
this.Day = int.Parse(a[1]);
22+
this.Year = int.Parse(a[2]);
23+
}
24+
25+
/// <summary>
26+
/// 构造函数。
27+
/// </summary>
28+
/// <param name="m">交易月份。</param>
29+
/// <param name="d">交易日。</param>
30+
/// <param name="y">交易年份。</param>
31+
public Date(int m, int d, int y)
32+
{
33+
this.Month = m;
34+
this.Day = d;
35+
this.Year = y;
36+
}
37+
38+
/// <summary>
39+
/// 返回形如 05/22/2017 的字符串。
40+
/// </summary>
41+
/// <returns></returns>
42+
public override string ToString()
43+
{
44+
return this.Month + "/" + this.Day + "/" + this.Year;
45+
}
46+
47+
/// <summary>
48+
/// 比较两个日期是否相同。
49+
/// </summary>
50+
/// <param name="obj">需要比较的另一个对象。</param>
51+
/// <returns></returns>
52+
public override bool Equals(object obj)
53+
{
54+
if (obj == this)
55+
return true;
56+
if (obj == null)
57+
return false;
58+
if (obj.GetType() != this.GetType())
59+
return false;
60+
Date that = (Date)obj;
61+
return (this.Year == that.Year) && (this.Month == that.Month) && (this.Day == that.Day);
62+
}
63+
64+
/// <summary>
65+
/// 获取日期的哈希值。
66+
/// </summary>
67+
/// <returns></returns>
68+
public override int GetHashCode()
69+
{
70+
int hash = 17;
71+
hash = 31 * hash + this.Month;
72+
hash = 31 * hash + this.Year;
73+
hash = 31 * hash + this.Day;
74+
return hash;
75+
}
76+
77+
/// <summary>
78+
/// 比较两个日期的先后。
79+
/// </summary>
80+
/// <param name="other">另一个日期。</param>
81+
/// <returns></returns>
82+
public int CompareTo(Date other)
83+
{
84+
if (this.Year > other.Year)
85+
return 1;
86+
else if (this.Year < other.Year)
87+
return -1;
88+
89+
if (this.Month > other.Month)
90+
return 1;
91+
else if (this.Month < other.Month)
92+
return -1;
93+
94+
if (this.Day > other.Day)
95+
return 1;
96+
else if (this.Day < other.Day)
97+
return -1;
98+
99+
return 0;
100+
}
101+
}
102+
}

2 Sorting/2.5/SortApplication/InsertionSort.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Diagnostics;
32

43
namespace SortApplication
54
{
@@ -26,9 +25,9 @@ public override void Sort<T>(T[] a)
2625
{
2726
Exch(a, j, j - 1);
2827
}
29-
Debug.Assert(IsSorted(a, 0, i));
28+
// Debug.Assert(IsSorted(a, 0, i));
3029
}
31-
Debug.Assert(IsSorted(a));
30+
// Debug.Assert(IsSorted(a));
3231
}
3332

3433
/// <summary>
@@ -46,9 +45,9 @@ public void Sort<T>(T[] a, IComparer<T> c)
4645
{
4746
Exch(a, j, j - 1);
4847
}
49-
Debug.Assert(IsSorted(a, 0, i, c));
48+
// Debug.Assert(IsSorted(a, 0, i, c));
5049
}
51-
Debug.Assert(IsSorted(a, c));
50+
// Debug.Assert(IsSorted(a, c));
5251
}
5352
}
5453
}

2 Sorting/2.5/SortApplication/QuickSort.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Diagnostics;
32

43
namespace SortApplication
54
{
@@ -22,7 +21,7 @@ public override void Sort<T>(T[] a)
2221
{
2322
Shuffle(a);
2423
Sort(a, 0, a.Length - 1);
25-
Debug.Assert(IsSorted(a));
24+
// Debug.Assert(IsSorted(a));
2625
}
2726

2827
/// <summary>

0 commit comments

Comments
 (0)