1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
/* Performing Binary Search on Java float Array Example This java example shows how to perform binary search for an element of java float array using Arrays class. */ import java.util.Arrays; public class BinarySearchFloatArrayExample { public static void main(String[] args) { //create float array float floatArray[] = {1.23f,2.10f,4.74f,5.34f}; /* To perform binary search on float array use int binarySearch(float[] b, float value) of Arrays class. This method searches the float array for specified float value using binary search algorithm. Please note that the float array MUST BE SORTED before it can be searched using binarySearch method. This method returns the index of the value to be searched, if found in the array. Otherwise it returns (- (X) - 1) where X is the index where the the search value would be inserted. i.e. index of first element that is grater than the search value or array.length, if all elements of an array are less than the search value. */ //sort float array using Arrays.sort method Arrays.sort(floatArray); //value to search float searchValue = 4.74f; //since 4.74 is present in float array, index of it would be returned int intResult = Arrays.binarySearch(floatArray,searchValue); System.out.println("Result of binary search of 4.74 is : " + intResult); //lets search something which is not in float array ! searchValue = 3.33f; intResult = Arrays.binarySearch(floatArray,searchValue); System.out.println("Result of binary search of 3.33 is : " + intResult); } } /* Output would be Result of binary search of 4.74 is : 2 Result of binary search of 3.33 is : -3 */ |
Add Comment