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;
		}
	}
}