Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Monday 26 March 2018

Binary Search Program in Java

Binary Search in Java Program

Write a Java Program for Binary Search

Here we are gonna to see binary search program in java which is mostly asked in core java interviews.

Binary search is used to search specified element from the list of multiple elements. Binary search algorithm is a algorithm that find the position of a targate value within a sorted array. It compares the targate value to the middle element of the array.

Binary search is also known as half-interval search or binary chop or logarithmic search.

Let's see binary search java example.


Write a Java Program for Binary Search 

This is the simple implementation of binary search using java program. Here we are going to use Scanner class for taking input from the user e.g number of elements, values of elements, and key element for searching.

import java.util.Scanner;
class BinarySearchExample
{
public static void main(String args[])
{
int num, array[], counter, item, first, last, middle;

Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements");
num = sc.nextInt();

array = new int[num];//creating array and declare number of elements

System.out.println("Enter " +num+ " Elements ");

for(counter = 0; counter < num; counter++)
array[counter] = sc.nextInt();

System.out.println("Enter key element for searching ");
item = sc.nextInt();
first = 0;
last = num - 1;
middle = (first + last)/2;

while(first <= last)
{

if(array[middle] < item )
first = middle + 1;

else if(array[middle] == item)
{
System.out.println(item+ " found at location " + (middle + 1));
break;
}

else
{
last = middle - 1;
}

middle = (first + last)/2;
}

if(first > last)
System.out.println(item+ " not found");
}
}

Output: Enter number of elements
             5
             Enter 5 elements
             14
             65
             89
             91
             96
             Enter key element for searching
             89
             89 found at location 3

There is another way we can perform binary search operationg using java program.


Binary Search Example

This is another java program for binary search by using Arrays.binarySearch() method.

We have to import java.util.Arrays class.

import java.util.Arrays;
class BinarySearch2
{
public static void main(String args[])
{
int elements[] = {14, 65, 89, 91, 96};
int keyElement = 89;

int search = Arrays.binarySearch(elements, keyElement);
if(search < 0)
{
System.out.println(" Element not found ");
}
else
{
System.out.println(" Element is found at index " + search);
}
}
}

Output: Element is found at index 2

You can check other java programs, link given below

Linear Search Program in Java
Bubble Sort Program in Java 
Java Array Programming Interview Questions 

Here we discussed some most important binary search programs in java which will help you in java interviews.


Share:

35 comments:

  1. Nice post.Thanks for sharing. Do check out our blog- We are a Java-Software development company in Frico.
    Java Software development Frisco

    ReplyDelete
    Replies
    1. Great Article android based projects

      Java Training in Chennai

      Project Center in Chennai

      Java Training in Chennai

      projects for cse

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. I have read your blog and I got very useful and knowledgeable information from your blog. It’s really a very nice articlejava training

    ReplyDelete
  3. thank you for sharing useful post.
    c++ programming tutorial
    welookups

    ReplyDelete
  4. Thanks for sharing such a nice information about Core Java .Keep sharing..

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Thanks for sharing valuable information with us.
    Java Training in OMR

    ReplyDelete
  7. Pretty! This has been a really wonderful post. Many thanks for providing this information.
    Java Training in Bangalore

    ReplyDelete
  8. Very impressive article! The blog is highly informative and has answered all my questions. To introduce about our company and the activities, Pegasi Media Group is a database provider that helps you to boost your sales & grow your business through well-build Java user list

    ReplyDelete
  9. I was more than happy to uncover this great site. I need to to thank you for your time due to this fantastic read!! I definitely enjoyed every bit of it and I have you bookmarked to see new information on your blog.
    Java Training in Bangalore

    ReplyDelete
  10. Learn online java here this is the best place for java programming

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. Tula's International School Best Boarding School in Dehradun

    Tula's International School Best Boarding School in Dehradun

    Tula's International School Co-ed Boarding School in Dehradun

    Tula's International School Best Residential School in Dehradun

    Tula's International School Dehradun Boarding School Fee structure

    Tula's International School Top Girls Boarding School India

    Tula's International School Best CBSE Schools in Uttarakhand

    Tula's International School Top Boarding Schools in India


    Tula's International School MCA College in Dehradun

    Tula's Institute Best Private BJMC College in Dehradun

    ReplyDelete
  13. Thanks for sharing the best information and suggestions, I love your content, and they are very nice and very useful to us. If you are looking for the best Java Tutorial, then visit w3spoint. I appreciate the work you have put into this.

    ReplyDelete
  14. Thank you for sharing wonderful information with us to get some idea about it.
    Workday Online Course
    Workday Online Training in India

    ReplyDelete
  15. I appreciate you finding the time and effort to put this article together reading and leaving comments. But so what, it was still
    worthwhile!
    Best Digital Marketing Agency !

    ReplyDelete
  16. R Programming Training In Delhi
    SASVBA provides the best R programming training in NCR Delhi with the best development environment and framework. We constantly update our program to reflect the latest industry trends. SASVBA is one of the top R programming institutes in Delhi (NCR) that helps technical students conduct interviews. We educate both college students and schoolchildren.

    ReplyDelete
  17. Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.Very useful and informative content has been shared out here, Thanks for sharing it.
    Visit us: Java Online Training Hyderabad
    Visit us: Core Java Online Course
    Visit us: java course

    ReplyDelete
  18. Thanks for sharing this blog with us. Good information and informative content.
    Visit us: Core Java Online Course
    Visit us: Best Online Java Course

    ReplyDelete
  19. It is so nice blog. I was really satisfied by seeing this blog.
    workday studio training
    workday studio online training

    ReplyDelete
  20. Excellent blog I visit this blog it's really awesome. The important thing is that in this blog content written clearly and understandable. The content of information is very informative.We are also providing the best services click on below links to visit our website.

    Oracle Fusion HCM Training
    Workday Training
    Okta Training
    Palo Alto Training
    Adobe Analytics Training

    ReplyDelete
  21. Great Post! Thanks for sharing. Keep sharing such information.
    Java Training

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate