Java Source Code: Binary Search in Recursion

Java Source Code: Binary Search in Recursion
4.0″ NEW Cheap Android 4.0 Dual Sim WIFI Capacitive Smart phone AT&T T-Mobile
Current Bid: $63.95

Below is the java source code for a recursive binary search. The program requires to sort the numbers first in an ascending order before the binary search can be implemented successfully. Basically, what the program does is prompt the user to input the size of the array, enter the numbers that the user wants, sort it, then ask for the number he wants to search. If the number is on the array, it outputs the index of the array where it is located but if it is not on the list is simple returns -1.Here how the program works.

How the program works

Important note: Binary Search algorithm would work only if the numbers are sorted.

For example:

User’s enters: 2 1 3 5 4

The sorted numbers are: 1 2 3 4 5

Search number: 2

The numbers is on the array index 1, since array index starts from 0. if the search number is 6, then the program returns -1, since 6 is not on the list.

Java Video Tutorials

Java Programming Video Training tutorials CBT 30+ Hrs
Current Bid: $4.99
Advanced Java Programming Video Training tutorials CBT – 30+ Hrs – Master level
Current Bid: $4.99
Learn JAVA language -14h video & docs tutorials
Current Bid: $4.59

Java Books

Introduction to Java Programming, Comprehensive Version 9e by Y. Daniel Liang
Current Bid: $50.80
Java Programming Lab Manual by Blayne Mayfield Third Edition
Current Bid: $14.99

How Binary Search Works?

1. It computes the middle index of the array

From the given numbers above, since there are 5 numbers, we will divide it into 2 to get the middle.

middle = 5/2, returns 2, since they are both integers so any decimal value will not be counted.

meaning on: 1 2 3 4 5, 3 is the middle, Since array index starts from 0.

2. Compare if the search number is equal to the middle if, yes return the index middle.

if(middle == search)

return index middle.

3. If not, it then compare if the search number is greater than or less than the middle. if it is greater than the value of the middle, the search starts at the middle + 1 number till at the last number in the array, however if the search number is less than the middle, it compares next the number located on the middle – 1 index value up to the first value of the array.

The Source code is given below.

Java Source code: How to implement Binary Search Using Recursion.

//Java Class
public class binarySearch //Example of Java Class
{
//biSearch is an example of a method or a function
public int binSearch(int[] arr, int fIndex, int lIndex,int search)
{
int middle = (fIndex + (lIndex – fIndex) / 2);
if(fIndexCool Android Phones!

Android Smart Phone MTK6515 CPU 1.0GHz 4.7 Inch Screen Galaxy S3 9300 Phone WIFI
Current Bid: $125.00
New LG VX9700 Dare Verizon/Page Plus Touch Screen GPS Cell Phone (No Contract)
Current Bid: $59.95
Motorola XPRT Sprint Android Smart Phone Rough Shape
Current Bid: $10.70

Cool Programmer Shirts

NEW Red Hat Linux Software Programmer T-shirt Size L Large Tee
Current Bid: $13.99
PROGRAMMER CAFFEINE INTO SOFTWARE NERD GAMER GEEK COFFEE CUP T-SHIRT
Current Bid: $10.99

Sample Output 1:

Enter the size of the array: 5

Enter 5 numbers: 2 1 3 5 4

The sorted numbers are:

1 2 3 4 5

Enter the number you want to search: 3

The search number is on the index 2

Sample Output 2:

Enter the size of the array: 5

Enter 5 numbers: 3 2 1 4 5

The sorted numbers are:

1 2 3 4 5

Enter the number you want to search: 1

The search number is on the index 0

Sample Output 3:

Enter the size of the array: 5

Enter 5 numbers: 2 1 3 5 4

The sorted numbers are:

1 2 3 4 5

Enter the number you want to search: 6

The search number is on the index -1

Java Tutorials and Tips

Java Tutorial Examples
5 Important Tips to Learn Java Programming and Other Programming Languages
Basic Knowledge Required in Programming
How to Program in Java: Complete Simple Easy Steps
Java Simple Codes for Beginners
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Java Class: Learn More About Classes in Java
facebook.com via google image

Other Java Source Codes

Java Simple Codes for Beginners
Java Source code: How to Add numbers inside an Array Using Recursion
Java Source code: How to Add Numbers inside an Array using For Loop
A Java source code: How to Output Asterisk in Different Forms Using Recursion
Java Source code: How to Print a String Backward using Recursion
Java Source code on Printing the Greatest Common Divisor (GCD) using Recursion
Java Source Code on a Recursive Linear Search
Here is the Java Source code for the Linear Search implemented in Recursion.
Java source code: How to Output the Answer of the Integer X to the Power Y
Java Source Code: How to make a Program that will determine a Person’s Salutation and Current Age
Java Source Code: How to Sort Numbers in Bubble Sort
Java Source Code: How to Sort Numbers using Selection Sort
Java Source Code: Recursive Snow Flakes

Do You Love Java?

Yes
No
See results without voting