Binary Search

Binary search is a search algorithm used to find the position of a specific item in a sorted list, or whether the element you are searching exist in an array or not. It does this by repeatedly dividing the search interval in half, ultimately narrowing down the search to a single element. Binary search is a very efficient algorithm with a run-time complexity of O(log n) that makes it ideal for searching large, sorted data sets.
How Binary Search Works
Binary search requires a sorted input list before it can proceed, it doesn't work on unsorted list. It starts by comparing the target value to the middle element of the list. If the target value is less than the middle value, the search continues in the lower half of the list. Similarly, if the target value is greater than the middle value, the search continues in the upper half of the list.
The search continues until either the target value is found or the sub-array being searched becomes empty. If the target value is not found, then it is not in the list.
Example Implementation in Java
Here's an example implementation of binary search in Java:
public static int binarySearch(int[] array, int target) {
int start = 0;
int end = array.length - 1; // as index always starts from 0
while (start <= end) { // if start becomes greater than end then the loop will terminates
int mid = start+(end-start) / 2; // you can also use (start+end)/2 but it is always efficient to use above recommended formula
if (target<arr[mid) {
end= mid-1;
if(target>arr[mid]){
start= mid+1;
} else { // you hsve found your ans i.e target== arr[mid]
return mid;
}
}
return -1; // if the complete loop is over and you didn't get your answer
}
The binary search function takes in an array of integers and the target value. It initializes the start and end indices to the first and the last element of the array, respectively. It then proceeds to loop through the sub-array by calculating a midpoint index and comparing the value at the midpoint to the target value.
If the midpoint value is equal to the target value, binary search returns the index of the midpoint. If the midpoint value is less than the target value, then the search continues in the upper half of the sub-array by moving the start index to the midpoint index plus one. If instead, the midpoint value is greater than the target value, the search continues in the lower half of the sub-array by moving the end index to the midpoint index minus one.
The function returns -1 if the target value is not found in the sub-array.
Conclusion
Binary search is a powerful search algorithm that is particularly well-suited for searching large, sorted datasets. It works by repeatedly dividing the search interval in half and can quickly narrow down the search to a single element. In this post, we provided an example implementation of binary search in Java. For more such blogs stay tuned..
About the author
I am currently an undergraduate in Computer Science and Engineering . I am currently working on my skills in java language and I also having knowledge of C language. Looking forward to improve my skills in android development and making some interesting projects .
Comments
Join the discussionNo comments yet. Be the first to comment.




