Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class ListExtensions
- {
- static int Find<TListInt>(this TListInt list, int first, int last, int value)
- where TListInt : IList<int>
- {
- int diff = ( last - first );
- if (diff == 0)
- {
- if (list[first] == value)
- {
- return last;
- }
- return -1;
- }
- int mid = first + ( diff / 2 );
- int midValue = list[mid];
- if (midValue == value)
- {
- return mid;
- }
- else if (midValue > value)
- {
- if (--mid < first)
- {
- return -1;
- }
- return list.Find(first, mid, value);
- }
- else
- {
- if (++mid > last)
- {
- return -1;
- }
- return list.Find(mid, last, value);
- }
- }
- public static int Find<TListInt>(this TListInt list, int value)
- where TListInt : IList<int>
- {
- if (list != null && list.Count > 0)
- {
- return list.Find(0, list.Count - 1, value);
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment