Skip to content

Commit 542af8e

Browse files
committed
#481, 3.1.41 Finished
1 parent f4bab15 commit 542af8e

File tree

6 files changed

+250
-1
lines changed

6 files changed

+250
-1
lines changed
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>{EE46F91D-3EB8-4E6C-B71F-10B3AF7CAB97}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>_3._1._41</RootNamespace>
10+
<AssemblyName>3.1.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+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
53+
</Project>

3 Searching/3.1/3.1.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>

3 Searching/3.1/3.1.41/Program.cs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using System;
2+
using System.Diagnostics;
3+
4+
namespace _3._1._41
5+
{
6+
/*
7+
* 3.1.41
8+
*
9+
* 插值查找的临界点。
10+
* 找出使用插值查找比二分查找要快 1 倍、2 倍和 10 倍的 N 值,
11+
* 其中假设所有键为随机的 32 二进位整数(请见练习 3.1.24)。
12+
* 分析并预测 N 的大小并通过实验验证它。
13+
*
14+
*/
15+
class Program
16+
{
17+
static long binarySearchCompare = 0;
18+
static long interpolationSearchCompare = 0;
19+
static Random random = new Random();
20+
21+
static void Main(string[] args)
22+
{
23+
// 原文:1, 2 and 10 times faster
24+
// 也就是一样快,快一倍和快十倍
25+
26+
// 对于均匀分布的数组,插值查找的平均需要 lg(lgN)) 次比较
27+
// 解方程
28+
// lg(lgN)) = lgN, N = 0.8..
29+
// 2lg(lgN)) = lgN, N = 4
30+
// 10lg(lgN)) = lgN, N = 58
31+
32+
Console.WriteLine("n\tist(compare/time)\tbst(compare/time)\tratio(compare/time)");
33+
Test(1, 10000000);
34+
Test(4, 10000000);
35+
Test(58, 10000000);
36+
}
37+
static void Test(int n, int trial)
38+
{
39+
binarySearchCompare = 0;
40+
interpolationSearchCompare = 0;
41+
Console.Write(n + "\t");
42+
43+
Stopwatch sw = new Stopwatch();
44+
45+
int[] data = new int[n];
46+
int[] dataSorted = new int[n];
47+
int[] dataQuery = new int[trial];
48+
for (int i = 0; i < n; i++)
49+
{
50+
dataSorted[i] = i;
51+
data[i] = i;
52+
}
53+
Shuffle(data);
54+
for (int i = 0; i < trial; i++)
55+
{
56+
dataQuery[i] = random.Next(0, n);
57+
}
58+
59+
60+
long bstTime = 0, istTime = 0;
61+
// 测试 ist
62+
sw.Start();
63+
for (int i = 0; i < trial; i++)
64+
{
65+
InterpolationSearch(data, dataQuery[i]);
66+
}
67+
sw.Stop();
68+
istTime = sw.ElapsedMilliseconds;
69+
Console.Write(interpolationSearchCompare + "/" + istTime + "\t\t");
70+
71+
// 测试 bst
72+
sw.Restart();
73+
for (int i = 0; i < trial; i++)
74+
{
75+
BinarySearch(dataSorted, dataQuery[i]);
76+
}
77+
sw.Stop();
78+
79+
bstTime = sw.ElapsedMilliseconds;
80+
Console.Write(binarySearchCompare + "/" + bstTime + "\t\t");
81+
82+
// 比例
83+
Console.WriteLine((float)interpolationSearchCompare / binarySearchCompare + "/" + (float)istTime / bstTime);
84+
}
85+
86+
static void Shuffle<T>(T[] array)
87+
{
88+
for (int i = 0; i < array.Length; i++)
89+
{
90+
int p = i + random.Next(array.Length - i);
91+
T temp = array[p];
92+
array[p] = array[i];
93+
array[i] = temp;
94+
}
95+
}
96+
97+
static int BinarySearch(int[] a, int key)
98+
{
99+
int lo = 0, hi = a.Length - 1;
100+
while (lo <= hi)
101+
{
102+
int mid = (lo + hi) / 2;
103+
int compare = a[mid].CompareTo(key);
104+
binarySearchCompare++;
105+
if (compare > 0)
106+
hi = mid - 1;
107+
else if (compare < 0)
108+
lo = mid + 1;
109+
else
110+
return mid;
111+
}
112+
return -1;
113+
}
114+
115+
116+
static int InterpolationSearch(int[] a, int key)
117+
{
118+
int lo = 0, hi = a.Length - 1;
119+
while (lo <= hi)
120+
{
121+
double percent = a[hi] == a[lo] ? 0 : (key - a[lo]) / (a[hi] - a[lo]);
122+
int index = lo + (int)Math.Floor((hi - lo) * percent);
123+
if (percent < 0)
124+
index = lo;
125+
if (percent > 1)
126+
index = hi;
127+
128+
int compare = a[index].CompareTo(key);
129+
interpolationSearchCompare++;
130+
if (compare > 0)
131+
hi = index - 1;
132+
else if (compare < 0)
133+
lo = index + 1;
134+
else
135+
return index;
136+
}
137+
return -1;
138+
}
139+
}
140+
}
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("3.1.41")]
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyProduct("3.1.41")]
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("ee46f91d-3eb8-4e6c-b71f-10b3af7cab97")]
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")]

3 Searching/3.1/SymbolTable/InterpolationSearchST.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public void Put(double key, int value)
227227
public int Rank(double key)
228228
{
229229
int lo = 0, hi = this.n - 1;
230-
while (lo <= hi)
230+
while (lo < hi)
231231
{
232232
double percent = (key - this.keys[lo]) / (this.keys[hi] - this.keys[lo]);
233233
int index = lo + (int)Math.Floor((hi - lo) * percent);

Algorithms 4th Edition.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "3.1.39", "3 Searching\3.1\3
960960
EndProject
961961
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "3.1.40", "3 Searching\3.1\3.1.40\3.1.40.csproj", "{6EA48AF4-541E-47D0-B1B7-E72BB3305773}"
962962
EndProject
963+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "3.1.41", "3 Searching\3.1\3.1.41\3.1.41.csproj", "{EE46F91D-3EB8-4E6C-B71F-10B3AF7CAB97}"
964+
EndProject
963965
Global
964966
GlobalSection(SolutionConfigurationPlatforms) = preSolution
965967
Debug|Any CPU = Debug|Any CPU
@@ -5866,6 +5868,18 @@ Global
58665868
{6EA48AF4-541E-47D0-B1B7-E72BB3305773}.Release|x64.Build.0 = Release|Any CPU
58675869
{6EA48AF4-541E-47D0-B1B7-E72BB3305773}.Release|x86.ActiveCfg = Release|Any CPU
58685870
{6EA48AF4-541E-47D0-B1B7-E72BB3305773}.Release|x86.Build.0 = Release|Any CPU
5871+
{EE46F91D-3EB8-4E6C-B71F-10B3AF7CAB97}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
5872+
{EE46F91D-3EB8-4E6C-B71F-10B3AF7CAB97}.Debug|Any CPU.Build.0 = Debug|Any CPU
5873+
{EE46F91D-3EB8-4E6C-B71F-10B3AF7CAB97}.Debug|x64.ActiveCfg = Debug|Any CPU
5874+
{EE46F91D-3EB8-4E6C-B71F-10B3AF7CAB97}.Debug|x64.Build.0 = Debug|Any CPU
5875+
{EE46F91D-3EB8-4E6C-B71F-10B3AF7CAB97}.Debug|x86.ActiveCfg = Debug|Any CPU
5876+
{EE46F91D-3EB8-4E6C-B71F-10B3AF7CAB97}.Debug|x86.Build.0 = Debug|Any CPU
5877+
{EE46F91D-3EB8-4E6C-B71F-10B3AF7CAB97}.Release|Any CPU.ActiveCfg = Release|Any CPU
5878+
{EE46F91D-3EB8-4E6C-B71F-10B3AF7CAB97}.Release|Any CPU.Build.0 = Release|Any CPU
5879+
{EE46F91D-3EB8-4E6C-B71F-10B3AF7CAB97}.Release|x64.ActiveCfg = Release|Any CPU
5880+
{EE46F91D-3EB8-4E6C-B71F-10B3AF7CAB97}.Release|x64.Build.0 = Release|Any CPU
5881+
{EE46F91D-3EB8-4E6C-B71F-10B3AF7CAB97}.Release|x86.ActiveCfg = Release|Any CPU
5882+
{EE46F91D-3EB8-4E6C-B71F-10B3AF7CAB97}.Release|x86.Build.0 = Release|Any CPU
58695883
EndGlobalSection
58705884
GlobalSection(SolutionProperties) = preSolution
58715885
HideSolutionNode = FALSE
@@ -6335,6 +6349,7 @@ Global
63356349
{A46FCA4D-9FD9-4172-8528-32BB93EBBCB8} = {F8C96526-F9C9-4A2B-9087-459F68745E2F}
63366350
{CCA70CC0-BFCE-4081-BF13-6A72C0270088} = {F8C96526-F9C9-4A2B-9087-459F68745E2F}
63376351
{6EA48AF4-541E-47D0-B1B7-E72BB3305773} = {F8C96526-F9C9-4A2B-9087-459F68745E2F}
6352+
{EE46F91D-3EB8-4E6C-B71F-10B3AF7CAB97} = {F8C96526-F9C9-4A2B-9087-459F68745E2F}
63386353
EndGlobalSection
63396354
GlobalSection(ExtensibilityGlobals) = postSolution
63406355
SolutionGuid = {EE55CF8D-6F35-464D-B95B-ED85644AE41C}

0 commit comments

Comments
 (0)