SlideShare a Scribd company logo
Java Assignment.
Implement a binary search algorithm on an array.
Solution
import java.util.Scanner;
class BinarySearch
{
public static void main(String args[])
{
int c, first, last, middle, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("Enter value to find");
search = in.nextInt();
first = 0;
last = n - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
System.out.println(search + " found at location " + (middle + 1) + ".");
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
System.out.println(search + " is not present in the list. ");
}
}

More Related Content

DOCX
Binary search
PPTX
Binary Searching Algorithm PowerPoint Presentation
PDF
In the binary search, if the array being searched has 32 elements in.pdf
PPTX
7 searching injava-binary
PPT
15-binary-search.ppt
PDF
Save Write a program to implement Binary search using recursive algo.pdf
DOCX
DS LAB RECORD.docx
PDF
Searching
Binary search
Binary Searching Algorithm PowerPoint Presentation
In the binary search, if the array being searched has 32 elements in.pdf
7 searching injava-binary
15-binary-search.ppt
Save Write a program to implement Binary search using recursive algo.pdf
DS LAB RECORD.docx
Searching

Similar to Java Assignment.Implement a binary search algorithm on an array..pdf (20)

PPT
Linear and Bianry search
DOCX
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPTX
Sorting and searching arrays binary search algorithm
PPTX
Searching.pptx
PPTX
Binary search algorithm.pptx
PPTX
Binary search
DOCX
lab 2 in microprocessior and coontrolew.docx
PPTX
Searching in DSA that follow a dsa searching.pptx
DOCX
JAVA - Design a data structure IntSet that can hold a set of integers-.docx
PPT
Binary search
PPT
Binary search in ds
PPTX
Searching linear &amp; binary search
PDF
Using Arrays with Sorting and Searching Algorithms1) This program .pdf
PDF
Binary search algorithm
PPTX
Chapter #3 (Searchinmmmg ALgorithm).pptx
PPT
working of binary search .BINARY_SEARCH.ppt
PDF
binary search
PPT
Searching in c language
Linear and Bianry search
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Sorting and searching arrays binary search algorithm
Searching.pptx
Binary search algorithm.pptx
Binary search
lab 2 in microprocessior and coontrolew.docx
Searching in DSA that follow a dsa searching.pptx
JAVA - Design a data structure IntSet that can hold a set of integers-.docx
Binary search
Binary search in ds
Searching linear &amp; binary search
Using Arrays with Sorting and Searching Algorithms1) This program .pdf
Binary search algorithm
Chapter #3 (Searchinmmmg ALgorithm).pptx
working of binary search .BINARY_SEARCH.ppt
binary search
Searching in c language
Ad

More from irshadoptical (20)

PDF
In practice, a RAM is used to simulate the operation of a stack by m.pdf
PDF
i need help to edit the two methods below so that i can have them wo.pdf
PDF
Help!! Which of the following correctly lists the simple molecules t.pdf
PDF
How does feasibility differ in an agile environment in comparison to.pdf
PDF
describe a real-life example of selection against a homozygous reces.pdf
PDF
Define a motor unit. What roles do motor units play in graded contrac.pdf
PDF
An antibody is a protein that has which level of protein structure .pdf
PDF
C++ PROGRAMThis program builds on the code below#include iostr.pdf
PDF
You are in charge of the investigation regarding possible exposur.pdf
PDF
Which of the following is not a product of the light reactions of ph.pdf
PDF
Write a line of code to create a Print Writer to write to the file .pdf
PDF
what is the difference bettween cotranslational import and posttrans.pdf
PDF
What is the meaning of critical periods in neonatal development Wha.pdf
PDF
What are the major differences between foreign bonds and Eurobonds .pdf
PDF
What is meant by ecological fallacySolutionAnswerThe term .pdf
PDF
What are the two main branches of Non-Euclidean Geometry What is the.pdf
PDF
By what transport method does oxygen enter the blood from the alveol.pdf
PDF
All of these are examples of evidence used to support the hypothesis.pdf
PDF
A 28 year-old biologist makes a trip to study life forms in a distan.pdf
PDF
4. Name the following compounds CO2 CH CH3 CH20H CH3COOH 5. Water ha.pdf
In practice, a RAM is used to simulate the operation of a stack by m.pdf
i need help to edit the two methods below so that i can have them wo.pdf
Help!! Which of the following correctly lists the simple molecules t.pdf
How does feasibility differ in an agile environment in comparison to.pdf
describe a real-life example of selection against a homozygous reces.pdf
Define a motor unit. What roles do motor units play in graded contrac.pdf
An antibody is a protein that has which level of protein structure .pdf
C++ PROGRAMThis program builds on the code below#include iostr.pdf
You are in charge of the investigation regarding possible exposur.pdf
Which of the following is not a product of the light reactions of ph.pdf
Write a line of code to create a Print Writer to write to the file .pdf
what is the difference bettween cotranslational import and posttrans.pdf
What is the meaning of critical periods in neonatal development Wha.pdf
What are the major differences between foreign bonds and Eurobonds .pdf
What is meant by ecological fallacySolutionAnswerThe term .pdf
What are the two main branches of Non-Euclidean Geometry What is the.pdf
By what transport method does oxygen enter the blood from the alveol.pdf
All of these are examples of evidence used to support the hypothesis.pdf
A 28 year-old biologist makes a trip to study life forms in a distan.pdf
4. Name the following compounds CO2 CH CH3 CH20H CH3COOH 5. Water ha.pdf
Ad

Recently uploaded (20)

PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Computing-Curriculum for Schools in Ghana
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
Classroom Observation Tools for Teachers
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
GDM (1) (1).pptx small presentation for students
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Pharma ospi slides which help in ospi learning
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Cell Types and Its function , kingdom of life
Microbial diseases, their pathogenesis and prophylaxis
Computing-Curriculum for Schools in Ghana
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
O7-L3 Supply Chain Operations - ICLT Program
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Classroom Observation Tools for Teachers
2.FourierTransform-ShortQuestionswithAnswers.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
GDM (1) (1).pptx small presentation for students
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Pharma ospi slides which help in ospi learning
human mycosis Human fungal infections are called human mycosis..pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Final Presentation General Medicine 03-08-2024.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Weekly quiz Compilation Jan -July 25.pdf
RMMM.pdf make it easy to upload and study
Abdominal Access Techniques with Prof. Dr. R K Mishra
STATICS OF THE RIGID BODIES Hibbelers.pdf
Cell Types and Its function , kingdom of life

Java Assignment.Implement a binary search algorithm on an array..pdf

  • 1. Java Assignment. Implement a binary search algorithm on an array. Solution import java.util.Scanner; class BinarySearch { public static void main(String args[]) { int c, first, last, middle, n, search, array[]; Scanner in = new Scanner(System.in); System.out.println("Enter number of elements"); n = in.nextInt(); array = new int[n]; System.out.println("Enter " + n + " integers"); for (c = 0; c < n; c++) array[c] = in.nextInt(); System.out.println("Enter value to find"); search = in.nextInt(); first = 0; last = n - 1; middle = (first + last)/2; while( first <= last ) { if ( array[middle] < search ) first = middle + 1; else if ( array[middle] == search ) { System.out.println(search + " found at location " + (middle + 1) + "."); break; } else last = middle - 1; middle = (first + last)/2;
  • 2. } if ( first > last ) System.out.println(search + " is not present in the list. "); } }