Logo

Programming-Idioms

History of Idiom 124 > diff from v28 to v29

Edit summary for version 29 by godbolt:
New Java implementation by user [godbolt]

Version 28

2019-01-24, 11:44:02

Version 29

2019-09-26, 19:49:45

Idiom #124 Binary search for a value in sorted array

Write function binarySearch which returns the index of an element having value x in sorted array a, or -1 if no such element.

Idiom #124 Binary search for a value in sorted array

Write function binarySearch which returns the index of an element having value x in sorted array a, or -1 if no such element.

Extra Keywords
dichotomic dichotomy
Extra Keywords
dichotomic dichotomy
Imports
import java.util.arrays;
Code
static int binarySearch(final int[] arr, final int key) {
    final int index = Arrays.binarySearch(arr, key);
    return index < 0 ? - 1 : index;
}