Skip to content

Commit

Permalink
Merge pull request #440 from ikesnowy/dev
Browse files Browse the repository at this point in the history
2.5 Finished
  • Loading branch information
ikesnowy committed Jan 27, 2019
2 parents 5f1fafb + 269d4e9 commit fb20e2a
Show file tree
Hide file tree
Showing 181 changed files with 46,019 additions and 10 deletions.
26 changes: 26 additions & 0 deletions 2 Sorting/2.3/Quick/QuickSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,32 @@ private int Partition<T>(T[] a, int lo, int hi) where T : IComparable<T>
return j;
}

/// <summary>
/// 令 a[k] 变成第 k 小的元素。
/// </summary>
/// <typeparam name="T">元素类型。</typeparam>
/// <param name="a">需要排序的数组。</param>
/// <param name="k">序号</param>
/// <returns></returns>
public T Select<T>(T[] a, int k) where T : IComparable<T>
{
if (k < 0 || k > a.Length)
throw new IndexOutOfRangeException("Select elements out of bounds");
Shuffle(a);
int lo = 0, hi = a.Length - 1;
while (hi > lo)
{
int i = Partition(a, lo, hi);
if (i > k)
hi = i - 1;
else if (i < k)
lo = i + 1;
else
return a[i];
}
return a[lo];
}

/// <summary>
/// 打乱数组。
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions 2 Sorting/2.4/PriorityQueue/MinPQ.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,21 @@ public void Insert(Key v)
public int Size() => this.n;

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

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

/// <summary>
/// 获取堆的迭代器,元素以降序排列
/// 获取堆的迭代器,元素以升序排列
/// </summary>
/// <returns></returns>
IEnumerator IEnumerable.GetEnumerator()
Expand Down
8 changes: 4 additions & 4 deletions 2 Sorting/2.4/PriorityQueue/MinPQX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,21 @@ public void Insert(Key v)
public int Size() => this.n;

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

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

/// <summary>
/// 获取堆的迭代器,元素以降序排列
/// 获取堆的迭代器,元素以升序排列
/// </summary>
/// <returns></returns>
IEnumerator IEnumerable.GetEnumerator()
Expand Down
53 changes: 53 additions & 0 deletions 2 Sorting/2.5/2.5.1/2.5.1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{CC3F5E6E-EE3B-41DA-8098-50D38CD55A7F}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>_2._5._1</RootNamespace>
<AssemblyName>2.5.1</AssemblyName>
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
6 changes: 6 additions & 0 deletions 2 Sorting/2.5/2.5.1/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
</startup>
</configuration>
24 changes: 24 additions & 0 deletions 2 Sorting/2.5/2.5.1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace _2._5._1
{
/*
* 2.5.1
*
* 在下面这段 String 类型的 compareTo() 方法的实现中,
* 第三行对提高运行效率有何帮助?
*
*/
class Program
{
static void Main(string[] args)
{
// if (this == that) return 0;
// 如果比较的两个 string 是同一个对象的引用,直接返回相等结果
// 而不必再逐字符比较。
string s = "123456";
string p = s;
Console.WriteLine(s.CompareTo(p)); // 输出 0
}
}
}
35 changes: 35 additions & 0 deletions 2 Sorting/2.5/2.5.1/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;

// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("2.5.1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("2.5.1")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]

// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("cc3f5e6e-ee3b-41da-8098-50d38cd55a7f")]

// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
54 changes: 54 additions & 0 deletions 2 Sorting/2.5/2.5.10/2.5.10.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{AE4E57EE-9891-4DDB-96B1-B875371BC222}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>_2._5._10</RootNamespace>
<AssemblyName>2.5.10</AssemblyName>
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Version.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
6 changes: 6 additions & 0 deletions 2 Sorting/2.5/2.5.10/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
</startup>
</configuration>
29 changes: 29 additions & 0 deletions 2 Sorting/2.5/2.5.10/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

namespace _2._5._10
{
/*
* 2.5.10
*
* 创建一个数据类型 Version 来表示软件的版本,
* 例如 155.1.1、155.10.1、155.10.2。
* 为它实现 Comparable 接口,
* 其中 115.1.1 的版本低于 115.10.1。
*
*/
class Program
{
static void Main(string[] args)
{
Version[] versions = new Version[3];
versions[0] = new Version("155.10.1");
versions[1] = new Version("155.1.1");
versions[2] = new Version("155.10.2");
Array.Sort(versions);
for (int i = 0; i < versions.Length; i++)
{
Console.WriteLine(versions[i]);
}
}
}
}
35 changes: 35 additions & 0 deletions 2 Sorting/2.5/2.5.10/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;

// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("2.5.10")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("2.5.10")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]

// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("ae4e57ee-9891-4ddb-96b1-b875371bc222")]

// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
43 changes: 43 additions & 0 deletions 2 Sorting/2.5/2.5.10/Version.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;

namespace _2._5._10
{
/// <summary>
/// 版本号。
/// </summary>
class Version : IComparable<Version>
{
private int[] versionNumber;

public Version(string version)
{
string[] versions = version.Split('.');
this.versionNumber = new int[versions.Length];
for (int i = 0; i < versions.Length; i++)
{
this.versionNumber[i] = int.Parse(versions[i]);
}
}

public int CompareTo(Version other)
{
for (int i = 0; i < this.versionNumber.Length && i < other.versionNumber.Length; i++)
{
if (this.versionNumber[i].CompareTo(other.versionNumber[i]) != 0)
return this.versionNumber[i].CompareTo(other.versionNumber[i]);
}
return this.versionNumber.Length.CompareTo(other.versionNumber.Length);
}

public override string ToString()
{
string result = "";
for (int i = 0; i < this.versionNumber.Length - 1; i++)
{
result += this.versionNumber[i] + ".";
}
result += this.versionNumber[this.versionNumber.Length - 1].ToString();
return result;
}
}
}
Loading

0 comments on commit fb20e2a

Please sign in to comment.