Binary search using recursive function

Only available on StudyMode
  • Download(s) : 35
  • Published : July 8, 2015
Open Document
Text Preview
Binary search using recursive function

Question:
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 constructor, void 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            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; iAdno[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

VARIABLEDISCRIPTION
Name of variable
Type of variable
Reason for it’s use
Ando
integer
It is used to take input of the elements from the user
i
integer
It is used to run the loop
j
integer
It is used to run the loop
temp
integer
It is used for swapping the value
l
integer
It is used to define the left end point of the search
u
integer
It is used to define the right end point of the search
v
integer
It is used to compare the searched value with other values
f
integer
It is used to check weather the condition is true or false

ALGORITHM
1.start
2.openning util package
3.defining the memory of the array Ando
4.creating the object
5.using constructor to...
tracking img