Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java -- Write a recursive, boolean -valued method named search that accepts an i

ID: 3568542 • Letter: J

Question

Java -- Write a recursive, boolean -valued method named search that accepts an integer array , the number of elements in the array , and an integer (in that order), and returns whether the integer is present as an element in the array . Searching for a particular value in an array can be performed in the following recursive manner:

--If the array has no elements , the value is not there.

--Compare the first element of the array to the value , if they're equal , the value is there; other search for the value in the rest of the array .

Explanation / Answer

//The java program to search for a value in a function search
//recursively and returns true if the element is found otherwise

//returns false
//RecursiveSearch.java
public class RecursiveSearch
{

   public static void main(String[] args)
   {

       //array of integers
       int elements[]={5,8,2,9,11};
       //size of the array
       int SIZE=5;
       //Search element
       int value=11;

      
       System.out.println("Array");
       for (int index = 0; index < elements.length; index++)
       System.out.print(elements[index]+" ");  
      
      
      
       System.out.println();
       System.out.println("Size of the array :"+SIZE);
       System.out.println("Searching element :"+value);
       //calling the method
       if(search(elements, SIZE, value))
           System.out.println("Element found .");
       else
           System.out.println("Element not found");

   }


   //The method search accepts an integer array ,size of the array and value to be
   //search in the array . The search is based on the size of the array
   //If the size of the value is greater than zero then check whether
   //the element is equal to value otherwise call the search method
   //recursively by decreasing the size value by one until the size
   //is only greater than zero .if the size is less than zero
   //then it returns false.
   private static boolean search(int elements[], int size, int value)
   {
       boolean found=false;
       if(size>0)
       {
           //check whether value is equal to the element
           //in the array
           if(elements[size-1]==value)
               //return true if the element is found
               return found=true;
       }

       else
           //call the search method
           return search(elements, size-1, value);
       //return false if the element is not found
       return found;

   }//end of the search method


}//end of the class
------------------------------------------------
//Sample output

Array
5 8 2 9 11
Size of the array :5
Searching element :11
Element found .

Hope this would be helpful

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote