0Pricing
Java Academy · Lesson

Arrays.binarySearch

Search sorted arrays.

Searching Sorted Arrays

Arrays.binarySearch finds an element in a sorted array in O(log n) time. It repeatedly halves the search range, which is far faster than scanning every element.

The Sorted Precondition

The array must already be sorted in ascending order. If it is not, the result is undefined. Always Arrays.sort first if you are unsure.

All lessons in this course

  1. Arrays.sort and Sorting
  2. Arrays.binarySearch
  3. Arrays.fill and copyOf
  4. Arrays.equals and toString
← Back to Java Academy