Binary Search using Recursion in Java

A class Binary contains an array of n integers (n ≤ 100) that are already arranged in ascending order. The subscripts of the array elements vary from 0 to n – 1.

The data members and member functions of class Binary are given below:
Class name: Binary
Data members:
a[]: integer array of 100 elements.
n: size of the array.
l: location of the lower bound
u: location of the upper bound
Member functions:
Binary(int nn): constructor to initialize the size n to nn and the other instance variables.
void readData(): to fill the elements of the array in ascending order.
int binarySearch(int v): returns the location of the value (v) to be searched in the list by using the binary search method using the recursive technique. The function returns -1 if the number is not present in the given list.

  1. Specify the class Binary giving details of the constructor, void readData() and
    int binarySearch(int). Write main() method.
  2. State the base case in the recursive technique binarySearch().
  3. What are the drawbacks of using the recursive technique?
import java.io.*;
class Binary{
    int a[];
    int n;
    int l;
    int u;
    public Binary(int nn){
        n = nn;
        if(n > 100)
            n = 100;
        a = new int[n];
        l = 0;
        u = n - 1;
    }
    public void readData()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter elements in ascending order:");
        for(int i = 0; i < n; i++)
            a[i] = Integer.parseInt(br.readLine());
        }
        public int binarySearch(int v){
            int mid = (l + u) / 2;
            if(a[mid] == v)
                return mid;
            else if(l > u)
                return -1;
        else if(v < a[mid])
            u = mid - 1;
        else
            l = mid + 1;
        return binarySearch(v);
    }
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the number of elements: ");
        int num = Integer.parseInt(br.readLine());
        Binary obj = new Binary(num);
        obj.readData();
        System.out.print("Enter element to search: ");
        int key = Integer.parseInt(br.readLine());
        int position = obj.binarySearch(key);
        if(position == -1)
            System.out.println(key + " unavailable.");
        else
            System.out.println(key + " available at position " + (position + 1));
    }
}

The base case is when v == a[mid] and when l > u.

The limitations of recursion is that it has a limited size as it maintains a stack. It also occupies more memory because every time the recursion occurs, it allocates extra memory to store the data.

4 thoughts on “Binary Search using Recursion in Java

  1. 2) A transpose of an array is obtained by interchanging the elements of rows and columns. A class
    Transarray contains a two dimensional integer array of order [ m x n]. The maximum value possible for
    both ‘m’ and ‘n’ is 20. Design a class Transarray to find the transpose of a given matrix. The details of the
    members of the class are given below:
    Class name : Trasarray
    Data members
    arr[][] : stores the matrix elements
    m : integer to store the number of rows.
    n : integer to store the number of columns.
    Member functions
    Transarray() : default constructor.
    Transarray(intmm,intnn) : to inititalize the size of the matrix, m=mm,n=nn.
    voidfillarray() : to enter elements into the matrix.
    void transpose(Transarray A) : to find the transpose of a given matrix.
    voiddisaparray() : displays the array in a matrix form.
    Specify the class Transarray giving details of the constructors, void fillarray(), void transpose(Transarray)
    and void disparray(). write the main function also.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.