Skip to content

Commit fb20e2a

Browse files
authored
Merge pull request #440 from ikesnowy/dev
2.5 Finished
2 parents 5f1fafb + 269d4e9 commit fb20e2a

File tree

181 files changed

+46019
-10
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

181 files changed

+46019
-10
lines changed

2 Sorting/2.3/Quick/QuickSort.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,32 @@ private int Partition<T>(T[] a, int lo, int hi) where T : IComparable<T>
6969
return j;
7070
}
7171

72+
/// <summary>
73+
/// 令 a[k] 变成第 k 小的元素。
74+
/// </summary>
75+
/// <typeparam name="T">元素类型。</typeparam>
76+
/// <param name="a">需要排序的数组。</param>
77+
/// <param name="k">序号</param>
78+
/// <returns></returns>
79+
public T Select<T>(T[] a, int k) where T : IComparable<T>
80+
{
81+
if (k < 0 || k > a.Length)
82+
throw new IndexOutOfRangeException("Select elements out of bounds");
83+
Shuffle(a);
84+
int lo = 0, hi = a.Length - 1;
85+
while (hi > lo)
86+
{
87+
int i = Partition(a, lo, hi);
88+
if (i > k)
89+
hi = i - 1;
90+
else if (i < k)
91+
lo = i + 1;
92+
else
93+
return a[i];
94+
}
95+
return a[lo];
96+
}
97+
7298
/// <summary>
7399
/// 打乱数组。
74100
/// </summary>

2 Sorting/2.4/PriorityQueue/MinPQ.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,21 @@ public void Insert(Key v)
120120
public int Size() => this.n;
121121

122122
/// <summary>
123-
/// 获取堆的迭代器,元素以降序排列
123+
/// 获取堆的迭代器,元素以升序排列
124124
/// </summary>
125125
/// <returns></returns>
126126
public IEnumerator<Key> GetEnumerator()
127127
{
128-
MaxPQ<Key> copy = new MaxPQ<Key>(this.n);
128+
MinPQ<Key> copy = new MinPQ<Key>(this.n);
129129
for (int i = 1; i <= this.n; i++)
130130
copy.Insert(this.pq[i]);
131131

132132
while (!copy.IsEmpty())
133-
yield return copy.DelMax(); // 下次迭代的时候从这里继续执行。
133+
yield return copy.DelMin(); // 下次迭代的时候从这里继续执行。
134134
}
135135

136136
/// <summary>
137-
/// 获取堆的迭代器,元素以降序排列
137+
/// 获取堆的迭代器,元素以升序排列
138138
/// </summary>
139139
/// <returns></returns>
140140
IEnumerator IEnumerable.GetEnumerator()

2 Sorting/2.4/PriorityQueue/MinPQX.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,21 +119,21 @@ public void Insert(Key v)
119119
public int Size() => this.n;
120120

121121
/// <summary>
122-
/// 获取堆的迭代器,元素以降序排列
122+
/// 获取堆的迭代器,元素以升序排列
123123
/// </summary>
124124
/// <returns></returns>
125125
public IEnumerator<Key> GetEnumerator()
126126
{
127-
MaxPQ<Key> copy = new MaxPQ<Key>(this.n);
127+
MinPQ<Key> copy = new MinPQ<Key>(this.n);
128128
for (int i = 1; i <= this.n; i++)
129129
copy.Insert(this.pq[i]);
130130

131131
while (!copy.IsEmpty())
132-
yield return copy.DelMax(); // 下次迭代的时候从这里继续执行。
132+
yield return copy.DelMin(); // 下次迭代的时候从这里继续执行。
133133
}
134134

135135
/// <summary>
136-
/// 获取堆的迭代器,元素以降序排列
136+
/// 获取堆的迭代器,元素以升序排列
137137
/// </summary>
138138
/// <returns></returns>
139139
IEnumerator IEnumerable.GetEnumerator()

2 Sorting/2.5/2.5.1/2.5.1.csproj

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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>{CC3F5E6E-EE3B-41DA-8098-50D38CD55A7F}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>_2._5._1</RootNamespace>
10+
<AssemblyName>2.5.1</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+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
53+
</Project>

2 Sorting/2.5/2.5.1/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.1/Program.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
3+
namespace _2._5._1
4+
{
5+
/*
6+
* 2.5.1
7+
*
8+
* 在下面这段 String 类型的 compareTo() 方法的实现中,
9+
* 第三行对提高运行效率有何帮助?
10+
*
11+
*/
12+
class Program
13+
{
14+
static void Main(string[] args)
15+
{
16+
// if (this == that) return 0;
17+
// 如果比较的两个 string 是同一个对象的引用,直接返回相等结果
18+
// 而不必再逐字符比较。
19+
string s = "123456";
20+
string p = s;
21+
Console.WriteLine(s.CompareTo(p)); // 输出 0
22+
}
23+
}
24+
}
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.1")]
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyProduct("2.5.1")]
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("cc3f5e6e-ee3b-41da-8098-50d38cd55a7f")]
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.5/2.5.10/2.5.10.csproj

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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>{AE4E57EE-9891-4DDB-96B1-B875371BC222}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>_2._5._10</RootNamespace>
10+
<AssemblyName>2.5.10</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="Version.cs" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<None Include="App.config" />
52+
</ItemGroup>
53+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
54+
</Project>

2 Sorting/2.5/2.5.10/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.10/Program.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
3+
namespace _2._5._10
4+
{
5+
/*
6+
* 2.5.10
7+
*
8+
* 创建一个数据类型 Version 来表示软件的版本,
9+
* 例如 155.1.1、155.10.1、155.10.2。
10+
* 为它实现 Comparable 接口,
11+
* 其中 115.1.1 的版本低于 115.10.1。
12+
*
13+
*/
14+
class Program
15+
{
16+
static void Main(string[] args)
17+
{
18+
Version[] versions = new Version[3];
19+
versions[0] = new Version("155.10.1");
20+
versions[1] = new Version("155.1.1");
21+
versions[2] = new Version("155.10.2");
22+
Array.Sort(versions);
23+
for (int i = 0; i < versions.Length; i++)
24+
{
25+
Console.WriteLine(versions[i]);
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)