Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

RECURSSION BASED PROGRAMS

Page No.:

PROGRAMS
BASED
ON
RECURSSIONS
RECURSSION BASED PROGRAMS
Page No.:

PROGRAM 1 : BINARY SEARCH


USING RECURSSION
Q. TO DESIGN A CLASS WITH THE FOLLOWING
SPECIFICATIONS :-

CLASS NAME :- Rebinary .

DATA MEMBERS :- An integer variable to store the limit of the array .

Another integer variable to store the lower limit of

the array . An integer to store the upper limit of the

array and an integer array.

MEMBER FUNCTIONS :- Rebinary( int nn ) - parametrized constructor to

assign 'nn' to lmt .

void readdata( ) - to read the numbers in the

array.

int binary_search(int v ) - to divide the sorted array

in two halves and to search the number i.e binary

search using recursive technique.

Write the main function for the program.


RECURSSION BASED PROGRAMS
Page No.:

1. ALGORITHM FOR THE PROGRAM REBINARY :-

STEP 1 : Start

STEP 2 : Declaring the class Rebinary

STEP 3 : Declaring varibles lmt, l, u and array a[ ] of integer

type as data members.

STEP 4 : Creating main( )method for the creation of the

object of the class Rebinary in order to call the

member functions.

STEP 5 : Creating an object 'r' when the parametrized

constructor is getting invoked.

Definition of parametrized constructor -

lmt nn
l 0
u lmt - 1
for i 0 to less than 100 step value 1
a[i] 0
End of parametrized constructor.

STEP 6 : Calling readdata( ) using the object r .

Definition of void readdata( ) -

printing ENTER THE NUMBERS.

for i 0 to less than lmt step value 1

accepting numbers from the user in a[ ].

End of readdata( ) .

STEP 7 : Printing enter the number to be searched

accepting the number from the user in 'k'.

STEP 8 : Calling binary_search(k) using the object r and

storing the value in variable 'p' of integer type.

Definition of int binary_search(int v)

Declaring mid of integer type


RECURSSION BASED PROGRAMS
Page No.:
mid (u+l)/2

if l <= v

if a[mid] == v

return (mid + 1)

else if a[mid] < v

l mid + 1
return binary_search( v )
else

u mid - 1;
return binary_search(v)
end of else if else
else

return -1;

End of binary_search( v )

STEP 9 : if p != -1

printing number k is present at position 'p'

else

printing number is not is present.

STEP 10 : End of main( )

STEP 11 : End.
RECURSSION BASED PROGRAMS
Page No.:

2. SOURCE CODE FOR THE PROGRAM REBINARY:-

import java.io.*;
class Rebinary // declaring the class
{
private int a[] = new int[100], lmt,l,u;
// declaring the data members
Rebinary(int nn) // parametrized constructor
{
lmt = nn;
l = 0; //initializing the data members.
u = lmt - 1;

for(int i = 0; i<100 ; i++)

{
a[i] = 0;
}
}
void readdata()throws IOException
// function to accept the elements from the user
{

System.out.println("Enter the numbers");

for(int i = 0; i<lmt; i++)


{
a[i] = Integer.parseInt(br.readLine());
// accepting the numbers from the user
}
}
int binary_search(int v)
// function to search the number by binary search technique
recursively
{
int mid = (u + l)/2 ;
if(l <= u)
{
if(a[mid] == v)
return (mid + 1) ;
RECURSSION BASED PROGRAMS
Page No.:
else if(a[mid]<v)
{
l = mid + 1;
return binary_search(v);
}
else
{
u = mid - 1;
return binary_search(v);
}
}
else
{
return -1;
}
}
private static BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
public static void main(String args[ ] )throws IOException
// declaring the main method to execute the member functions
{
int k;
System.out.println("Enter the size of the array");
// accepting the size of the array
int n = Integer.parseInt(br.readLine());
Rebinary r = new Rebinary(n);
r.readdata( );
System.out.println("Enter the number to be searched");
// accepting the number to be searched
k = Integer.parseInt(br.readLine());
int p = r.binary_search(k) ;
if(p != -1)
System.out.println("Number is present at the position:- "+p);
else
System.out.println("Number is not present");
} //end of main function
} // end of class Rebinary
RECURSSION BASED PROGRAMS
Page No.:

3. OUTPUT FOR THE PROGRAM BINARY SEARCH BASED ON RECURSIONS :-


RECURSSION BASED PROGRAMS
Page No.:

4. VARIABLE CHART FOR THE PROGRAM BINARY SEARCH BASED ON


RECURSION :-

USAGE OF THE
VARIABLE : DATA TYPE :
VARIABLE:
r Rebinary Encapsulation.
a[] int To store the elements.
To store the size of
lmt int
the array.
To store the index of
u int the topmost element
of the array.
To store the index of
l int the lowermost element
of the array.
Used as a counter
i int
variable.
To store the index of
mid cell between the
mid int
cells represented by u
and l.
To accept the value of
limit in the
parameterized
nn int
constructor and then
pass its value to
variable lmt.
To store the number
k int
to be searched.
To accept the size of
n int array as per users
choice.
To store the position
p int of the searched
element else stores -1.

You might also like