Tuesday 27 October 2015

Binary Search Using Recursion

A class Admission contain the admission numbers of 100 students. Some of the data members/ member functions are given below:
Class name: Admission
Data member/instance variable:
Adno[ ]: Integer array to store admission numbers
Member functions/methods:
Admission(): constructur to initialize the array elements
void fillArray(): to accept the element of the array in ascending order
int binSearch(int l, int u, int v): to search for a particular admission number(v) using binary search and recursive technique and return 1 if found otherwise returns -1
Specify the class Admission giving details of the constructorvoid fillArrray() and int binSearch(int, int, int). Define the main() function to create an object and call the functions accordingly to enable task.

import java.util.*;
class Admission
{
    int Adno[]=new int[100];
    static Scanner sc = new Scanner(System.in);
 
    Admission() // Default constructor
    {
        for(int i=0; i<100; i++)
        {
            Adno[i]=0;
        }
    }
     
    void fillArray()throws Exception // Function to accept elements in ascending order
    {
        for(int i=0; i<100; i++)
        {
            System.out.print("Enter Admission no of student "+(i+1)+": ");
            Adno[i] = sc.nextInt();
        }
         
        /*Sorting the array in ascending order */
         
        int temp=0;
        for(int i=0; i<99; i++)
        {
            for(int j=i+1; j<100; j++)
            {
                if(Adno[i]>Adno[j])
                {
                    temp = Adno[i];
                    Adno[i] = Adno[j];
                    Adno[j] = temp;
                }
            }
        }           
    }
     
    int binSearch(int l, int u, int v) // Recursive function implementing binary search
    {
        int mid = (l + u)/2;
        if(u < l) // condition if the search is unsuccessful
        {
            return -1;
        }
        if(v==Adno[mid]) // condition if the search is successful
        {
            return 1;
        }
        else if(v>Adno[mid])
        {
            return binSearch(mid+1,u,v);
        }
        else
        {
            return binSearch(l,mid-1,v);
        }
    }
     
    public static void main(String args[])throws Exception
    {
        Admission ob = new Admission();
        System.out.println("Enter Admission number in ascending order");
        ob.fillArray();
        System.out.print("Enter an Admission number to search : ");
        int v = sc.nextInt();
        int f = ob.binSearch(0,99,v);
        System.out.println("*****************************");
        if(f == 1)
        {
            System.out.println("Admission Number found");
        }
        else
        {
            System.out.println("Admission Number Not found");
        }
    }
}

Output:

Note: The output has been taken for 5 elements in the array
Enter Admission number in ascending order
Enter Admission no of student 1: 205
Enter Admission no of student 2: 310
Enter Admission no of student 3: 670
Enter Admission no of student 4: 887
Enter Admission no of student 5: 952
Enter an Admission number to search : 887
*****************************
Admission Number found

No comments:

Post a Comment