Preview

Binary search using recursive function

Good Essays
Open Document
Open Document
777 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Binary search using recursive function
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<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])

You May Also Find These Documents Helpful