- 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 long array
Binary search on a long array can be implemented by using the method java.util.Arrays.binarySearch(). This method returns the index of the required long 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) { long long_arr[] = { 250L, 500L, 175L, 90L, 415L }; Arrays.sort(long_arr); System.out.print("The sorted array is: "); for (long i : long_arr) { System.out.print(i + " "); } System.out.println(); int index1 = Arrays.binarySearch(long_arr, 415L); System.out.println("The long value 415 is at index " + index1); int index2 = Arrays.binarySearch(long_arr, 50L); System.out.println("The long value 50 is at index " + index2); } }
Output
The sorted array is: 90 175 250 415 500 The long value 415 is at index 3 The long value 50 is at index -1
Now let us understand the above program.
The long array long_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 −
long long_arr[] = { 250L, 500L, 175L, 90L, 415L }; Arrays.sort(long_arr); System.out.print("The sorted array is: "); for (long i : long_arr) { System.out.print(i + " "); } System.out.println();
The method Arrays.binarySearch() is used to find the index of element 415 and 50. Since 415 is in the array, its index is displayed. Also, 50 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(long_arr, 415L); System.out.println("The long value 415 is at index " + index1); int index2 = Arrays.binarySearch(long_arr, 50L); System.out.println("The long value 50 is at index " + index2);
- Related Articles
- Java Program to implement Binary Search on char array
- Java Program to implement Binary Search on double 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 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)
- Java program to Sort long Array
