- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to implement Binary Search on double array
Binary search on a double array can be implemented by using the method
java.util.Arrays.binarySearch(). This method returns the index of the required double element if it is available in the array, otherwise it returns (-(insertion point) - 1) where the insertion point is the position at which the element would be inserted into the array.
A program that demonstrates this is given as follows −
Example
import java.util.Arrays; public class Demo { public static void main(String[] args) { double d_arr[] = { 5.2, 7.5, 9.7, 1.8, 4.0 }; Arrays.sort(d_arr); System.out.print("The sorted array is: "); for (double i : d_arr) { System.out.print(i + " "); } System.out.println(); int index1 = Arrays.binarySearch(d_arr, 9.7); System.out.println("The double value 9.7 is at index " + index1); int index2 = Arrays.binarySearch(d_arr, 2.5); System.out.println("The double value 2.5 is at index " + index2); } }
Output
The sorted array is: 1.8 4.0 5.2 7.5 9.7 The double value 9.7 is at index 4 The double value 2.5 is at index -2
Now let us understand the above program.
The double array d_arr[] is defined and then sorted using Arrays.sort(). Then the sorted array is printed using for loop. A code snippet which demonstrates this is as follows −
double d_arr[] = { 5.2, 7.5, 9.7, 1.8, 4.0 }; Arrays.sort(d_arr); System.out.print("The sorted array is: "); for (double i : d_arr) { System.out.print(i + " "); } System.out.println();
The method Arrays.binarySearch() is used to find the index of element 9.7 and 2.5. Since 9.7 is in the array, its index is displayed. Also, 2.5 is not in the array and so the value according to (-(insertion point) - 1) is displayed. A code snippet which demonstrates this is as follows −
int index1 = Arrays.binarySearch(d_arr, 9.7); System.out.println("The double value 9.7 is at index " + index1); int index2 = Arrays.binarySearch(d_arr, 2.5); System.out.println("The double value 2.5 is at index " + index2);
- Related Articles
- Java Program to implement Binary Search on char array
- Java Program to implement Binary Search on long array
- Java Program to implement Binary Search on float array
- Java Program to implement Binary Search on an array
- Java program to implement binary search
- C++ Program to Implement Randomized Binary Search Tree
- Python Program to Implement Binary Search without Recursion
- Python Program to Implement Binary Search with Recursion
- How to perform binary search on an array in java?
- Java program to implement linear search
- C++ Program to Implement self Balancing Binary Search Tree
- C++ Program to Implement Double Order Traversal of a Binary Tree
- C++ Program to Implement a Binary Search Algorithm for a Specific Search Sequence
- C++ Program to Implement a Binary Search Tree using Linked Lists
- Java Program for Binary Search (Recursive)
