binary search java
Java
// Java implementation of iterative Binary Search
class BinarySearch {
// Returns index of x if it is present in arr[],
// else return -1
int binarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr[m] == x)
return m;
// If x greater, ignore left half
if (arr[m] < x)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
// if we reach here, then element was
// not present
return -1;
}
// Driver method to test above
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, x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at "
+ "index " + result);
}
}
Also in Java:
- Title
- java hashmap put
- Category
- Java
- Title
- online money transfer andhra bank
- Category
- Java
- Title
- how to get the dimensions of a 2d array in java
- Category
- Java
- Title
- java random seed
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- java scanner next()
- Category
- Java
- Title
- raise number to power java
- Category
- Java
- Title
- read from elasticsearch in spark
- Category
- Java
- Title
- creating array java
- Category
- Java
- Title
- java treemap
- Category
- Java
- Title
- java program to get all cat indices in elasticsearch javadoc
- Category
- Java
- Title
- how to read to into a file in java
- Category
- Java
- Title
- calculate days between two dates in java
- Category
- Java
- Title
- java min function
- Category
- Java
- Title
- java get command line input
- Category
- Java
- Title
- java quit application
- Category
- Java
- Title
- casting java
- Category
- Java
- Title
- how to add a keylistener to a jframe
- Category
- Java
- Title
- java open file
- Category
- Java
- Title
- java get first char
- Category
- Java
- Title
- iterator loop java
- Category
- Java
- Title
- remove space string java
- Category
- Java
- Title
- biginteger in java
- Category
- Java
- Title
- eliminar fila jtable java
- Category
- Java
- Title
- get last element of array java
- Category
- Java
- Title
- how to return the lower of two values in one line java
- Category
- Java
- Title
- how to create a method java
- Category
- Java
- Title
- array in java
- Category
- Java
- Title
- java get keys from hashmap
- Category
- Java
- Title
- java localdate subtract two dates
- Category
- Java
- Title
- convert string to byte java
- Category
- Java
- Title
- how to substring in java
- Category
- Java
- Title
- java 8 add whole array to jsonarray
- Category
- Java
- Title
- has been compiled by a more recent version of the Java Runtime (class file version 56.0), this version of the Java Runtime only recognizes class file versions up to 55.0
- Category
- Java
- Title
- java loop through arraylist
- Category
- Java
- Title
- java read each lines in file
- Category
- Java
- Title
- print a string java
- Category
- Java
- Title
- java logical operator
- Category
- Java
- Title
- java initialize float to zero
- Category
- Java
- Title
- set java
- Category
- Java
- Title
- cordova cannot find symbol import com.google.firebase.iid.FirebaseMessagingService;
- Category
- Java
- Title
- recursion in java
- Category
- Java
- Title
- spannable string in android java for color
- Category
- Java
- Title
- java fx display simple rectangle
- Category
- Java
- Title
- java random numbers in specific range
- Category
- Java
- Title
- applicationcontext.xml
- Category
- Java
- Title
- binary to int java
- Category
- Java
- Title
- java djikstra's algorithm
- Category
- Java
- Title
- primitive data types in java
- Category
- Java
- Title
- how to check the end of a string java
- Category
- Java
- Title
- file with line numbers inserted java
- Category
- Java
- Title
- java 8 loop in map
- Category
- Java
- Title
- how to make array of objects in java and use it
- Category
- Java
- Title
- arraylist java methds
- Category
- Java
- Title
- reverse array in java
- Category
- Java
- Title
- how to return the first character in an array from a method java
- Category
- Java
- Title
- java map foreach
- Category
- Java
- Title
- initiralizing an arraylist java
- Category
- Java
- Title
- java run project from command line
- Category
- Java
- Title
- connecting to h2 database from java
- Category
- Java
- Title
- scanner check if int
- Category
- Java
- Title
- java string to double
- Category
- Java
- Title
- java execute jar from main
- Category
- Java
- Title
- arraylist to int array java
- Category
- Java
- Title
- add view to relativelayout programmatically
- Category
- Java
- Title
- Android recycleView
- Category
- Java
- Title
- java regex replace all characters before
- Category
- Java
- Title
- java windowbuilder full screen
- Category
- Java
- Title
- java create clas
- Category
- Java
- Title
- java string split underscore
- Category
- Java
- Title
- convert date to calendar java
- Category
- Java
- Title
- java split string on two or more spaces except for words in quotes
- Category
- Java
- Title
- how to check if a string is in alphabetical order in java
- Category
- Java
- Title
- JSONObject java
- Category
- Java
- Title
- java shortest if else statement
- Category
- Java
- Title
- how to open a folder in java swing project
- Category
- Java
- Title
- validate isbn number java
- Category
- Java
- Title
- nullpointer extension
- Category
- Java
- Title
- show menuitem when menu hovered javafx
- Category
- Java
- Title
- java check if directory exists
- Category
- Java
- Title
- java list get first element
- Category
- Java
- Title
- java hashmap set value
- Category
- Java
- Title
- what is java
- Category
- Java
- Title
- selection sort in java
- Category
- Java
- Title
- hashmap in java
- Category
- Java
- Title
- java print type of object
- Category
- Java
- Title
- how to convert string to double in android studio
- Category
- Java
- Title
- room insert and return id
- Category
- Java
- Title
- java comments
- Category
- Java
- Title
- initialize scanner java
- Category
- Java
- Title
- how to make a pre set list java
- Category
- Java
- Title
- set iteration java
- Category
- Java
- Title
- java get size of array
- Category
- Java
- Title
- priority queue java reverse order
- Category
- Java
- Title
- how to create a linked list in java
- Category
- Java
- Title
- kotlin jsonobject get nested
- Category
- Java
- Title
- read csv in java in spring
- Category
- Java
- Title
- tower of hanoi program in java using recursion
- Category
- Java
- Title
- catch array out of bounds exception java
- Category
- Java
- Title
- java swing button on click
- Category
- Java