diff --git a/inf3.csproj b/inf3.csproj index 53d5c28..07c6a7a 100644 --- a/inf3.csproj +++ b/inf3.csproj @@ -113,6 +113,7 @@ Form + diff --git a/src/BinarySearch.cs b/src/BinarySearch.cs new file mode 100644 index 0000000..004ff78 --- /dev/null +++ b/src/BinarySearch.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; + +namespace WorldOfPeacecraft +{ + public class BinarySearch + { + public static int search (T searchedElement, IList list, Func 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; + } + } +} \ No newline at end of file