Binary Search
This commit is contained in:
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