示例#1
0
        public int find(int itemToFind, IList <int> collection)
        {
            if (!collection.Any())
            {
                return(-1);
            }

            var foundIndex = 0;

            switch (itemToFind.CompareTo(collection[collection.Count / 2]))
            {
            case 0:
                return(collection.Count / 2);

            case 1:
                foundIndex = binary_chopper.find(itemToFind, collection.Skip(collection.Count / 2 + 1).ToList());
                return(foundIndex != -1 ? foundIndex + collection.Count / 2 + 1 : -1);

            case -1:
                foundIndex = binary_chopper.find(itemToFind, collection.Take(collection.Count / 2).ToList());
                return(foundIndex != -1 ? foundIndex : -1);
            }

            throw new Exception();
        }