Binary Search

This commit is contained in:
2014-05-23 09:06:23 +02:00
parent 3dd6628718
commit 56c7d821a3
2 changed files with 29 additions and 0 deletions

View File

@@ -113,6 +113,7 @@
<Compile Include="src\Gui\Splashscreen.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="src\BinarySearch.cs" />
<Compile Include="Sorttest.cs" />
</ItemGroup>
<ItemGroup>

28
src/BinarySearch.cs Normal file
View 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;
}
}
}