Week 34 — How would one implement a binary search algorithm?
Question of the Week #34
How would one implement a binary search algorithm?
8 Replies
Binary search is an algorithm for finding a specific element in a sorted list or array.
It works by first taking a look at the element in the "middle" of the data structure. If the found element is the element in the middle, the position of the element is found immediately.
If the element to look for is smaller than the "middle" element, it continues searching through the elements on the left and repeats the procedure.
Similarly, in case the element to look for is greater than the element middle element, it continues searching on the right.
The advantage of binary search is that it does not need to check each element individually but only a fraction of the elements because it uses the fact that the data structure is sorted.
More formally, the algorithm has a runtime complexity of O(log(n)) meaning that for n elements, it only needs logarithm of n instructions ignoring constant factors.
With an
int[]
, this can be implemented as follows:
If we want to search through a list of arbitrary objects, we need a way to compary two objects i.e. whether they are considered equal or which one of them is smaller/bigger. For that, we use the Comparator
interface which provides the method compare
. comparator.compare(a,b)
returns a value >0
iff a
is greater than b
, <0
iff a
is smaller than b
and 0
if they should be considered equal:
Since sorting is a fundamental operation used by many programs, Java actually provides methods like this to search through sorted arrays and lists. For applying a binary search on a sorting array, one can use
Arrays.binarySearch
, e.g. Arrays.binarySearch(new int[]{1,2,3,4,5,6},5)
.
In order to use a binary search for a list of elements, one can use the Collections.binarySearch
method which is provided by Java as well, e.g. Collections.binarySearch(List.of("a","b","c","d","e"),"d",Comparator.naturalOrder())
.
Note that these methods don't always return -1
if the element was not found but actually a negative value that also represents the position where the element would need to be inserted.⭐ Submission from dan1st#0000
Binary search is a very powerful algorithm, as it allows search in O(log n) time complexity. However the down side is that you need a sorted array, and sorting the array may require more computational resources, but with efficient sorting algorithms like quick sort, binary search is still a good choice.
Submission from friskaunderscore#0000
What do i get for answear?
Submission from kazaruma#0000
A binary search is a type of search algorithm that is optimized to work well with comparable elements in a sorted set. The set being sorted allows it to easily figure out which elements are where, since you can compare the element to be searched with the current element being looked at, and figure out of the element being searched should be at a lower index or at a higher index.
The first step of a binary search is to compare the searched element with the middle element. If the searched element is smaller than the middle element, you can figure out that the searched element must be in the lower half of the list, since the list is sorted. If not, the element has to be in the upper half. Then, the algorithm visits the middle of the new region and compares that with the searched element, and from there on out you just repeat. An example on how that would work for a set of integers:
Of course you need to keep track of your current position in the list, an easy way to figure that out is to store the current region's start index and length.
For the first iteration in the example, the index would be 0 and the length would be the entire length of the list, 7.
For the second+ iteration, the index would be either the start index of the previous iteration, or the start index + (length - 1) / 2 + 1, depending on if you branched left or right. The length would be (previousLength - 1) / 2. This continues onward for every next iteration.
A general java implementation should look something like this:
⭐ Submission from 0x150#0000