- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Java Source Code: Binary Search in Recursion
Get A Website Plus a Free Domain Name in Just 1 Hour!
Bring the new technology in your hands! Share your skills, improve and impress. Get Your Own Website and a Free Domain Name Here!
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.
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(fIndex<lIndex ){ if (search == arr[middle]){ return middle; } else if(search < arr[middle]){ if(search == arr[0]) return 0; return binSearch(arr, fIndex, middle, search); } else if(search > arr[middle]){ if(search == arr[middle+1]) return middle + 1; return binSearch(arr, middle+1, lIndex, search); } } return -1; } //this is also a class method public void sort(int[] arr) { for(int i=0; i<arr.length; i++) { for(int j=i+1; j<arr.length; j++ ) { if(arr[i] > arr[j]) { int temp = arr[j]; arr[j]=arr[i]; arr[i]= temp; } } } for(int i=0; i<arr.length; i++) { System.out.print(arr[i] + " "); } } } //main class import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the size of the array: "); int n = input.nextInt(); int[] x = new int[n]; System.out.print("Enter "+ n +" numbers: "); int middle; for(int i=0; i<n; i++) { x[i] = input.nextInt(); } binarySearch access = new binarySearch(); //this is how to instantiate an object to access a class System.out.println("The sorted numbers are: "); access.sort(x);//this is how to access a method System.out.println(); System.out.print("Enter the number you want to search: "); int value = input.nextInt(); System.out.print("The search number is on the index "); System.out.print(access.binSearch(x, 0, x.length-1, value)); //how to access a class } }
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
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