Skip to content

Commit cb23993

Browse files
committed
3.2.39 Finished
1 parent 97b0ccd commit cb23993

File tree

4 files changed

+914
-1
lines changed

4 files changed

+914
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.0</TargetFramework>
6+
<RootNamespace>_3._2._39</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\BinarySearchTree\BinarySearchTree.csproj" />
11+
</ItemGroup>
12+
13+
</Project>

3 Searching/3.2/3.2.39/Program.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using BinarySearchTree;
3+
4+
namespace _3._2._39
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
var n = 10000;
11+
var trial = 100;
12+
for (var i = 0; i < 3; i++)
13+
{
14+
var odds = new int[n];
15+
var evens = new int[n];
16+
var bst = new BSTAnalysis<int, int>();
17+
for (var j = 100; j < n; j++)
18+
{
19+
evens[j] = j;
20+
odds[j] = j + 1;
21+
}
22+
Shuffle(odds);
23+
foreach (var item in odds)
24+
{
25+
bst.Put(item, item);
26+
}
27+
28+
Console.WriteLine("n:" + n);
29+
// hit
30+
Shuffle(odds);
31+
Test(bst, odds, trial, "hit");
32+
33+
// miss
34+
Shuffle(evens);
35+
Test(bst, evens, trial, "miss");
36+
37+
n *= 10;
38+
}
39+
}
40+
41+
static void Test(BSTAnalysis<int, int> bst, int[] testCases, int trials, string label)
42+
{
43+
var testRecords = new long[trials];
44+
for (var j = 0; j < trials; j++)
45+
{
46+
bst.CompareTimes = 0; // reset
47+
bst.Get(testCases[j]); // test
48+
testRecords[j] = bst.CompareTimes; // record
49+
}
50+
51+
var testAverage = 0d; // 'd' for double
52+
foreach (var record in testRecords)
53+
{
54+
testAverage += record;
55+
}
56+
57+
testAverage /= testRecords.Length;
58+
59+
var testStandardDeviation = 0d;
60+
foreach (var record in testRecords)
61+
{
62+
testStandardDeviation += (record - testAverage) * (record - testAverage);
63+
}
64+
65+
testStandardDeviation /= testRecords.Length;
66+
testStandardDeviation = Math.Sqrt(testStandardDeviation);
67+
// 2lnN + 2γ - 3
68+
var expect = 2 * Math.Log(testCases.Length) + 2 * 0.5772156649 - 3;
69+
Console.WriteLine(label + ": ActualAverage: " + testAverage + "\tExpectAverage: " + expect + "\tStandardDevitation:" + testStandardDeviation);
70+
}
71+
72+
static void Shuffle<T>(T[] a)
73+
{
74+
var random = new Random();
75+
for (var i = 0; i < a.Length; i++)
76+
{
77+
var r = i + random.Next(a.Length - i);
78+
var temp = a[i];
79+
a[i] = a[r];
80+
a[r] = temp;
81+
}
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)