Binary Search in Java Program
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.
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.
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.
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.
Nice post.Thanks for sharing. Do check out our blog- We are a Java-Software development company in Frico.
ReplyDeleteJava Software development Frisco
Great Article android based projects
DeleteJava 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
nice post
ReplyDeleteconsoledude
superb post as usual
ReplyDeleteI have read your blog and I got very useful and knowledgeable information from your blog. It’s really a very nice articlejava training
ReplyDeleteNice article
ReplyDeleteJava programming
thank you for sharing useful post.
ReplyDeletec++ programming tutorial
welookups
Thanks for sharing such a nice information about Core Java .Keep sharing..
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThanks for sharing valuable information with us.
ReplyDeleteJava Training in OMR
Thanks for your informative blog java training in chennai
ReplyDeleteWebsite designing Company Shamli
ReplyDeleteWebsite Designing Company Varanasi
Web Designing Gorakhpur
Website designing Company Ghazipur
Website designing Company Dehradun
Website designing Company Shahjahanpur
Website designing Company Lucknow
Pretty! This has been a really wonderful post. Many thanks for providing this information.
ReplyDeleteJava Training in Bangalore
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
ReplyDeletethanks.
ReplyDeletedistance between delhi to kasauli by road
tourist places in india for summer
holiday destinations near delhi
best tourist places in india
hill station tour packages
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.
ReplyDeleteJava Training in Bangalore
Learn online java here this is the best place for java programming
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteTula's International School Best Boarding School in Dehradun
ReplyDeleteTula'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
Great Blog. Thnaks.
ReplyDeleteSAP Training in Chennai
Java Training in Chennai
Software Testing Training in Chennai
.Net Training in Chennai
Hardware and Networking Training in Chennai
AWS Training in Chennai
Azure Training in Chennai
Selenium Training in Chennai
QTP Training in Chennai
Android Training in Chennai
"It was an informative post indeed. Now It's the time to make the switch to solar power,
ReplyDeletecontact us(National Solar Company) today to learn more about how solar power works.
battery storage solar
united solar energy
solar panels
solar inverter
solar batteries
solar panels adelaide
best solar panels
solar power
battery storage solar
battery charger solar
solar regulators
solar charge controllers
solar battery storage
instyle solar
solar panels melbourne
solar panels for sale
solar battery charger
solar panels cost
buy solar panels"
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.
ReplyDeleteThank you for sharing wonderful information with us to get some idea about it.
ReplyDeleteWorkday Online Course
Workday Online Training in India
I appreciate you finding the time and effort to put this article together reading and leaving comments. But so what, it was still
ReplyDeleteworthwhile!
Best Digital Marketing Agency !
R Programming Training In Delhi
ReplyDeleteSASVBA 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.
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.
ReplyDeleteVisit us: Java Online Training Hyderabad
Visit us: Core Java Online Course
Visit us: java course
python online training
ReplyDeletepython training
Great Article.Thanks for the remarkable information. Please keep updating us.
ReplyDeleteWebsite Designing Company in Meerut
Website Designing Company in Delhi
best hoarding company in india
News Today
best magazine in india
Thanks for sharing this blog with us. Good information and informative content.
ReplyDeleteVisit us: Core Java Online Course
Visit us: Best Online Java Course
nice post.devops online training
ReplyDeleteIt is so nice blog. I was really satisfied by seeing this blog.
ReplyDeleteworkday studio training
workday studio online training
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.
ReplyDeleteOracle Fusion HCM Training
Workday Training
Okta Training
Palo Alto Training
Adobe Analytics Training
Im obliged for the blog article.Thanks Again. Awesome.
ReplyDeletecore java course
core java online Training
Great Post! Thanks for sharing. Keep sharing such information.
ReplyDeleteJava Training
nice information thanks for sharing this article very useful for me ................!
ReplyDeleteui path training
google cloud data engineer certification
micro strategy certification training
Nice Article!
ReplyDeleteThanks for sharing with us 🙂
SAT Coaching in Hyderabad