SlideShare a Scribd company logo
Save Write a program to implement Binary search using recursive algorithm. Submit a
screenshot of your Java source codes and IDE/Comment line results in PDF file.
Solution
BinarySearchTest.java
import java.util.Scanner;
public class BinarySearchTest {
public static void main(String[] args) {
int[] arr1 = { 1,2,3,4,5,6,7,8,9,10};
Scanner scan = new Scanner(System.in);
System.out.println("Enter the key: ");
int key = scan.nextInt();
int index = binarySearch(arr1,0,arr1.length,key);
System.out.println("Index is "+index);
}
public static int binarySearch(int[] array, int a, int e, int key) {
if (a < e) {
int mid = a + (e - a) / 2;
if (key < array[mid]) {
return binarySearch(array, a, mid, key);
} else if (key > array[mid]) {
return binarySearch(array, mid+1, e , key);
} else {
return mid;
}
}
return -1;
}
}
Output:
Enter the key:
4
Index is 3

More Related Content

Similar to Save Write a program to implement Binary search using recursive algo.pdf (20)

PDF
Java 7 New Features
Jussi Pohjolainen
 
PPTX
Top 20 java programming interview questions for sdet
DevLabs Alliance
 
PPTX
Static code analysis: what? how? why?
Andrey Karpov
 
PDF
Java Simple Programs
Upender Upr
 
PPT
arrays
teach4uin
 
PPTX
Static analysis: Around Java in 60 minutes
Andrey Karpov
 
PPTX
Java Language fundamental
Infoviaan Technologies
 
PDF
Workshop "Can my .NET application use less CPU / RAM?", Yevhen Tatarynov
Fwdays
 
DOCX
object oriented programming lab manual .docx
Kirubaburi R
 
PPTX
6_Array.pptx
shafat6712
 
PPT
compiler design
Ishwor2
 
PPTX
Unit 3
GOWSIKRAJAP
 
PDF
Average.javaimport java.util.Scanner;   import java.util.Arra.pdf
anuragperipheral
 
PDF
CS3381 OBJECT ORIENTED PROGRAMMINGLABS_1.pdf
deepak14367
 
PPT
Using xUnit as a Swiss-Aarmy Testing Toolkit
Chris Oldwood
 
DOC
Java final lab
Vivek Kumar Sinha
 
PDF
C# p9
Renas Rekany
 
ZIP
とある断片の超動的言語
Kiyotaka Oku
 
DOCX
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
dorisc7
 
PDF
TestR: generating unit tests for R internals
Roman Tsegelskyi
 
Java 7 New Features
Jussi Pohjolainen
 
Top 20 java programming interview questions for sdet
DevLabs Alliance
 
Static code analysis: what? how? why?
Andrey Karpov
 
Java Simple Programs
Upender Upr
 
arrays
teach4uin
 
Static analysis: Around Java in 60 minutes
Andrey Karpov
 
Java Language fundamental
Infoviaan Technologies
 
Workshop "Can my .NET application use less CPU / RAM?", Yevhen Tatarynov
Fwdays
 
object oriented programming lab manual .docx
Kirubaburi R
 
6_Array.pptx
shafat6712
 
compiler design
Ishwor2
 
Unit 3
GOWSIKRAJAP
 
Average.javaimport java.util.Scanner;   import java.util.Arra.pdf
anuragperipheral
 
CS3381 OBJECT ORIENTED PROGRAMMINGLABS_1.pdf
deepak14367
 
Using xUnit as a Swiss-Aarmy Testing Toolkit
Chris Oldwood
 
Java final lab
Vivek Kumar Sinha
 
とある断片の超動的言語
Kiyotaka Oku
 
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
dorisc7
 
TestR: generating unit tests for R internals
Roman Tsegelskyi
 

More from arihantmobileselepun (20)

PDF
How are the epimysium and tendons relatedA. The epimysium surroun.pdf
arihantmobileselepun
 
PDF
How does DNA differ from RNAA. RNA uses only purinesB. RNA uses.pdf
arihantmobileselepun
 
PDF
Gerard throws a discus distances of 10 meters, 14.5 meters, 14.8 met.pdf
arihantmobileselepun
 
PDF
For Printing and personal use ONLY. DO NOT Hand in this copy of the a.pdf
arihantmobileselepun
 
PDF
Find and provide a link to a multi-year capital plan from any Illino.pdf
arihantmobileselepun
 
PDF
A geneticist is working with a new bacteriophage called phage Y3 that.pdf
arihantmobileselepun
 
PDF
Assume that the four living species in the figure below evolved from.pdf
arihantmobileselepun
 
PDF
Declining BalanceThe cost of the asset $10,000.00 The salvage v.pdf
arihantmobileselepun
 
PDF
Create a java project that - Draw a circle with three random init.pdf
arihantmobileselepun
 
PDF
aphase The spindle contracts and the sister chromatids are separated.pdf
arihantmobileselepun
 
PDF
A population of wild deer mice includes individuals with long or sho.pdf
arihantmobileselepun
 
PDF
Who is it important that the lymphatic system operate separately f.pdf
arihantmobileselepun
 
PDF
Provide a full explanation to the below question.1. Summarize the .pdf
arihantmobileselepun
 
PDF
Write a program to generate the entire calendar for one year. The pr.pdf
arihantmobileselepun
 
PDF
Write the interval notation for the set of numbers graphed. Solu.pdf
arihantmobileselepun
 
PDF
Why doesnt hemoglobin have any intermediate conformations between .pdf
arihantmobileselepun
 
PDF
4. What is gyanandromorphy How does this happenSolutionAnswe.pdf
arihantmobileselepun
 
PDF
Which of the following requires the mitochondria to create contact po.pdf
arihantmobileselepun
 
PDF
why nitrogen fixation is so importantSolutionNitrogen fixation.pdf
arihantmobileselepun
 
PDF
what will happen to the photon if two photons enter the same atom si.pdf
arihantmobileselepun
 
How are the epimysium and tendons relatedA. The epimysium surroun.pdf
arihantmobileselepun
 
How does DNA differ from RNAA. RNA uses only purinesB. RNA uses.pdf
arihantmobileselepun
 
Gerard throws a discus distances of 10 meters, 14.5 meters, 14.8 met.pdf
arihantmobileselepun
 
For Printing and personal use ONLY. DO NOT Hand in this copy of the a.pdf
arihantmobileselepun
 
Find and provide a link to a multi-year capital plan from any Illino.pdf
arihantmobileselepun
 
A geneticist is working with a new bacteriophage called phage Y3 that.pdf
arihantmobileselepun
 
Assume that the four living species in the figure below evolved from.pdf
arihantmobileselepun
 
Declining BalanceThe cost of the asset $10,000.00 The salvage v.pdf
arihantmobileselepun
 
Create a java project that - Draw a circle with three random init.pdf
arihantmobileselepun
 
aphase The spindle contracts and the sister chromatids are separated.pdf
arihantmobileselepun
 
A population of wild deer mice includes individuals with long or sho.pdf
arihantmobileselepun
 
Who is it important that the lymphatic system operate separately f.pdf
arihantmobileselepun
 
Provide a full explanation to the below question.1. Summarize the .pdf
arihantmobileselepun
 
Write a program to generate the entire calendar for one year. The pr.pdf
arihantmobileselepun
 
Write the interval notation for the set of numbers graphed. Solu.pdf
arihantmobileselepun
 
Why doesnt hemoglobin have any intermediate conformations between .pdf
arihantmobileselepun
 
4. What is gyanandromorphy How does this happenSolutionAnswe.pdf
arihantmobileselepun
 
Which of the following requires the mitochondria to create contact po.pdf
arihantmobileselepun
 
why nitrogen fixation is so importantSolutionNitrogen fixation.pdf
arihantmobileselepun
 
what will happen to the photon if two photons enter the same atom si.pdf
arihantmobileselepun
 
Ad

Recently uploaded (20)

PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Ad

Save Write a program to implement Binary search using recursive algo.pdf

  • 1. Save Write a program to implement Binary search using recursive algorithm. Submit a screenshot of your Java source codes and IDE/Comment line results in PDF file. Solution BinarySearchTest.java import java.util.Scanner; public class BinarySearchTest { public static void main(String[] args) { int[] arr1 = { 1,2,3,4,5,6,7,8,9,10}; Scanner scan = new Scanner(System.in); System.out.println("Enter the key: "); int key = scan.nextInt(); int index = binarySearch(arr1,0,arr1.length,key); System.out.println("Index is "+index); } public static int binarySearch(int[] array, int a, int e, int key) { if (a < e) { int mid = a + (e - a) / 2; if (key < array[mid]) { return binarySearch(array, a, mid, key); } else if (key > array[mid]) { return binarySearch(array, mid+1, e , key); } else { return mid; } } return -1; } } Output: Enter the key: 4