From 56c7d821a343c764e819ccbc9c0d84fc3c8f666c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Fri, 23 May 2014 09:06:23 +0200 Subject: [PATCH] Binary Search --- inf3.csproj | 1 + src/BinarySearch.cs | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 src/BinarySearch.cs 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