Skip to content

Commit f353c67

Browse files
committed
Three ways quick search
1 parent 0cbe3f3 commit f353c67

File tree

5 files changed

+80
-6
lines changed

5 files changed

+80
-6
lines changed

Algorithms/Algorithms.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<TargetFramework>netstandard1.4</TargetFramework>
44
</PropertyGroup>
55
<ItemGroup>
6+
<Folder Include="DataStructures\SearchTries" />
67
<Folder Include="Selection" />
78
</ItemGroup>
89
<ItemGroup>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using Algorithms.Utils;
3+
4+
namespace Algorithms.Strings.Sort
5+
{
6+
public class ThreeWaysQuickSort
7+
{
8+
public static void Sort(string[] a)
9+
{
10+
Sort(a, 0, a.Length-1, 0);
11+
}
12+
13+
private static void Sort(string[] a, int lo, int hi, int d)
14+
{
15+
if (lo >= hi)
16+
{
17+
return;
18+
}
19+
20+
var c = charAt(a[lo], d);
21+
22+
int i = lo, lt = lo, gt = hi;
23+
while (i <= gt)
24+
{
25+
var cmp = c.CompareTo(charAt(a[i], d));
26+
if(cmp > 0) SortUtil.Exchange(a, i++, lt++);
27+
else if (cmp < 0) SortUtil.Exchange(a, i, gt--);
28+
else i++;
29+
}
30+
31+
Sort(a, lo, lt-1, d);
32+
if(c >= 0) Sort(a, lt, gt, d+1);
33+
Sort(a, gt+1, hi, d);
34+
}
35+
36+
private static int charAt(string a, int d)
37+
{
38+
if (a.Length <= d)
39+
{
40+
return -1;
41+
}
42+
return a[d];
43+
}
44+
}
45+
}

AlgorithmsUnitTest/Strings/Sort/LSDUnitTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ public void Test()
2020
var words= "bed bug dad yes zoo now for tip ilk dim tag jot sob nob sky hut men egg few jay owl joy rap gig wee was wad fee tap tar dug jam all bad yet".Split(' ');
2121
LSD.Sort(words);
2222

23-
for (var i = 0; i < words.Length; ++i)
23+
for (var i = 1; i < words.Length; ++i)
2424
{
25-
console.WriteLine(words[i]);
26-
Console.WriteLine(words[i]);
25+
Assert.True(words[i-1].CompareTo(words[i]) <= 0);
2726
}
2827

2928
}

AlgorithmsUnitTest/Strings/Sort/MSDUnitTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ public void Test()
2020
var words= "bed bug dad yes zoo now for tip ilk dim tag jot sob nob sky hut men egg few jay owl joy rap gig wee was wad fee tap tar dug jam all bad yet".Split(' ');
2121
MSD.Sort(words);
2222

23-
for (var i = 0; i < words.Length; ++i)
23+
for (var i = 1; i < words.Length; ++i)
2424
{
25-
console.WriteLine(words[i]);
26-
Console.WriteLine(words[i]);
25+
Assert.True(words[i-1].CompareTo(words[i]) <= 0);
2726
}
2827

2928
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using Algorithms.Strings.Sort;
3+
using Xunit;
4+
using Xunit.Abstractions;
5+
6+
namespace AlgorithmsUnitTest.Strings.Sort
7+
{
8+
public class ThreeWaysQuickSortUnitTest
9+
{
10+
private ITestOutputHelper console;
11+
public ThreeWaysQuickSortUnitTest(ITestOutputHelper console)
12+
{
13+
this.console = console;
14+
}
15+
16+
[Fact]
17+
public void Test()
18+
{
19+
20+
var words= "bed bug dad yes zoo now for tip ilk dim tag jot sob nob sky hut men egg few jay owl joy rap gig wee was wad fee tap tar dug jam all bad yet".Split(' ');
21+
ThreeWaysQuickSort.Sort(words);
22+
23+
for (var i = 1; i < words.Length; ++i)
24+
{
25+
Assert.True(words[i-1].CompareTo(words[i]) <= 0);
26+
}
27+
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)