A Java Programming learning website packed with information and programs ranging from basics
, arrays
, string programs
to 2D arrays
, recursion
, matrices
and many more.
class BinarySearch {
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr, 0, n - 1, x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at index " + result);
}
}
Element to Search : 3
Element found at index : 1
Our docs are designed to make learning Java easy and fun.
Talk is cheap. Show me the code.
The people who are crazy enough to think they can change the world are the ones who do.
I choose a lazy person to do a hard job. Because a lazy person will find a easy way to do it.
Everything that has not been hyperlinked will soon be hyperlinked.
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Truth can only be found in one place: the code.
Java is to JavaScript what car is to Carpet.
Code is like humor. When you have to explain it, it’s bad.
Our maintainers devote their time, effort, and heart to ensure Javaistic keeps getting better. Support us by donating to our collective 🙏
Sponsor the Javaistic maintainers
Sponsor the Javaistic maintainers on GitHub