Binary Search
This commit is contained in:
@@ -113,6 +113,7 @@
|
|||||||
<Compile Include="src\Gui\Splashscreen.cs">
|
<Compile Include="src\Gui\Splashscreen.cs">
|
||||||
<SubType>Form</SubType>
|
<SubType>Form</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="src\BinarySearch.cs" />
|
||||||
<Compile Include="Sorttest.cs" />
|
<Compile Include="Sorttest.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
28
src/BinarySearch.cs
Normal file
28
src/BinarySearch.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace WorldOfPeacecraft
|
||||||
|
{
|
||||||
|
public class BinarySearch<T>
|
||||||
|
{
|
||||||
|
public static int search (T searchedElement, IList<T> list, Func<T, T, bool> isSmaller)
|
||||||
|
{
|
||||||
|
int start = 0;
|
||||||
|
int end = list.Count - 1;
|
||||||
|
int current;
|
||||||
|
do {
|
||||||
|
current = start + (end - start) / 2;
|
||||||
|
if (isSmaller(list[current], searchedElement)) {
|
||||||
|
start = current;
|
||||||
|
}
|
||||||
|
else if (isSmaller(searchedElement, list[current])) {
|
||||||
|
end = current;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
} while (end - start > 0);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user