Binary Search on an Array
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
byte bArray[] = { 1, 2, 4, 5 };
Arrays.sort(bArray);
byte searchValue = 2;
int intResult = Arrays.binarySearch(bArray, searchValue);
System.out.println("Result of binary search of 2 is : " + intResult);
searchValue = 7;
intResult = Arrays.binarySearch(bArray, searchValue);
System.out.println("Result of binary search of 3 is : " + intResult);
}
}
Performing Binary Search on Java char Array Example
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
char charArray[] = { 'a', 'b', 'd', 'e' };
Arrays.sort(charArray);
char searchValue = 'b';
System.out.println(Arrays.binarySearch(charArray, searchValue));
searchValue = 'z';
System.out.println(Arrays.binarySearch(charArray, searchValue));
}
}
Performing Binary Search on Java double Array Example
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
double doubleArray[] = { 1.3, 2.1, 4.7, 5.3 };
Arrays.sort(doubleArray);
double searchValue = 4.7;
System.out.println(Arrays.binarySearch(doubleArray, searchValue));
searchValue = 3.33;
System.out.println(Arrays.binarySearch(doubleArray, searchValue));
}
}
Performing Binary Search on Java float Array Example
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
float floatArray[] = { 1.2f, 2.1f, 4.7f, 5.3f };
Arrays.sort(floatArray);
float searchValue = 4.7f;
System.out.println(Arrays.binarySearch(floatArray, searchValue));
searchValue = 3.3f;
System.out.println(Arrays.binarySearch(floatArray, searchValue));
}
}
Performing Binary Search on Java int Array Example
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int intArray[] = { 1, 2, 4, 5 };
Arrays.sort(intArray);
int searchValue = 2;
System.out.println(Arrays.binarySearch(intArray, searchValue));
searchValue = 3;
System.out.println(Arrays.binarySearch(intArray, searchValue));
}
}
Performing Binary Search on Java long Array Example
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
long longArray[] = { 1L, 2L, 4L, 5L };
Arrays.sort(longArray);
long searchValue = 2L;
System.out.println(Arrays.binarySearch(longArray, searchValue));
searchValue = 3;
System.out.println(Arrays.binarySearch(longArray, searchValue));
}
}
Performing Binary Search on Java short Array
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
short shortArray[] = { 1, 2, 4, 5 };
Arrays.sort(shortArray);
short searchValue = 2;
System.out.println(Arrays.binarySearch(shortArray, searchValue));
searchValue = 3;
System.out.println(Arrays.binarySearch(shortArray, searchValue));
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
Data Type Array:
- Append item to array
- Append an object to an array.
- Append one array to another
- Clone Array
- Clones two dimensional float array
- Compare two arrays by reference
- Compare Two byte Arrays
- Compare Two boolean Arrays
- Compare Two char Arrays
- Compare Two double Arrays
- Compare Two float Arrays
- Compare Two int Arrays
- Compare Two long Arrays
- Compare two object arrays
- Compare Two short Arrays
- Compare two two-dimensional array
- Concatenate arrays
- Convert array to set
- Convert string array to string
- Convert string array to List
- Convert multi-dimensional array to string
- Convert an array to a Map
- Copy array
- Copy Elements from One Array to Another
- Copy and add an array at the end of the new array
- Extend an array with additional extra space
- Extend to Double the size of an array
- Fill boolean, byte, char,short, int, long, float array
- Get array upperbound
- Get the number of dimensions
- Insert an Element into a Sorted Array
- Insert the specified element at the specified position in the array
- Insert source array in the target array at offset
- Maximum value in an array
- Minimum value in an array.
- Merge two arrays
- Remove duplicate element from array
- Remove the element at the specified position from the specified array.
- Reverses the order of an array(byte, long, int)
- Search an element in an array and return the index and last index
- Binary Search on an Array
- Shift all elements right by one
- Shift all elements left by one
- Shuffle an array
- Sort byte(char,double, float, int) Array
- Sort char array
- Sort int Array
- Sort long Array
- Sort short Array
- Sort string type array in case insensitive order and case sensitive order
- Sort an Array in Descending (Reverse) Order
- Start with one array
- Subarray from array that starts at offset