Performs a binary search for the specified element in the specified ascending sorted array. Searching in an unsorted array has an undefined result. It's also undefined which element is found if there are multiple occurrences of the same element. @param array the sorted byte array
(byte[] array, byte value)
| 179 | * is {@code -index - 1} where the element would be inserted. |
| 180 | */ |
| 181 | public static int binarySearch(byte[] array, byte value) { |
| 182 | int low = 0, mid = -1, high = array.length - 1; |
| 183 | while (low <= high) { |
| 184 | mid = (low + high) >>> 1; |
| 185 | if (value > array[mid]) { |
| 186 | low = mid + 1; |
| 187 | } else if (value == array[mid]) { |
| 188 | return mid; |
| 189 | } else { |
| 190 | high = mid - 1; |
| 191 | } |
| 192 | } |
| 193 | if (mid < 0) { |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | return -mid - (value < array[mid] ? 1 : 2); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Performs a binary search for the specified element in the specified |
nothing calls this directly
no test coverage detected