1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| int BinarySearch(List<int> list, int target) { int low = 0; int high = list.Count - 1; while (low <= high) { int mid = (low + high) / 2; int tmp = list[mid]; if(tmp == target) return mid; if(tmp > target) high = mid - 1; else low = mid + 1; } return -1; }
int pos = BinarySearch(new List<int>{1, 3, 5, 7, 9}, 3);
|