Question

In: Computer Science

Problem 2 Write a program in Java to implement a recursive search function int terSearch(int A[],...

Problem 2

Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x)

that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1.

The terSearch search function, unlike the binary search, must consider two dividing points

int d1 = l + (r - l)/3

int d2 = d1 + (r - l)/3

For the first call of your recursive search function terSearch you must consider l = 0 and r = A.length - 1.

Important Notes:

• For this problem you must add the main method in your program in order to test your implementation.

  • There are no data errors that need to be checked as all the data will be assumed correct.

• A program that does not compile or does not run loses all correctness points.

Solutions

Expert Solution

/**
* Main java program is to test the
methodterSearch. In the main method, initialize the array with 10 elements in * *sorted order.Then call themethod ,terSearch Then print the results of seach method on console output.
* */

//Main.java
public class Main

{
   public static void main(String[] args)
   {  
       //initialize an array of integer values
       int A[]= {10,20,30,40,50,60,70,80,90,100};
       //Set target search ,x=100
       int x=100;

       //Set left most index,l=0
       int l=0;
       //Set the right most index,r which is size of the array
       int r=A.length-1;

       System.out.println("Searching for "+x);
       //calling method ,terSearch
       int result =terSearch(A, l, r, x);

       if(result!=-1)
           System.out.println(x+" element is found at index location : "+result);
       else
           System.out.println(x+" is not found.");

      
       //set x=500
       x=500;
       System.out.println("Searching for "+x);
       //calling method ,terSearch
       result =terSearch(A, l, r, x);

       if(result!=-1)
           System.out.println(x+" element is found at index location : "+result);
       else
           System.out.println(x+" is not found.");

   }

   /*The method terSearch takes four arguments :
   * An array of integer type, l for left most index, r for right most index
   * and x is the target element to search in the method.
   * The method returns -1 if the element x is not found
   * otherwise returns the index location of the element ,x found in the array*/
   public static int terSearch(int A[], int l, int r, int x)
   {
       /*Base -case */
       /*Returns -1 if the left most index,l is greater than right most index,r*/
       if ( l > r )
           return -1;

       /*Calculate the d1 and d2*/
       int d1 = l + ((r - l) / 3);
       int d2 = d1 + ((r - l) / 3);

       //Case-1:
       /*Checking element at index,d1 is equals to target element,x
       * then return,d1 value*/
       if (A[d1] == x)
           return d1;

       //Case-2:
       /*Checking element at index,d2 is equals to target element,x
       * then return,d2 value*/
       if (A[d2] == x)
           return d2;

       //Case-3:
       /*Checking if element,x is less than element at index,d1
       * then the call the recursive function terSearch in the range of l to d1-1 */
       if ( x < A[d1])
           return terSearch(A, l, d1 - 1, x);
       //Case-4:
       /*Checking if element,x is greater than element at index,d2
       * then the call the recursive function terSearch in the range of d2+1 to r */
       else if ( x > A[d2])
           return terSearch(A, d2 + 1, r, x);
       else
           //case-5:
           /*Otherwise the target element, x is in the range of d1 and d2 where d1 is left most index
           * and d2 is the right most index.
           * calling the method terSearch*/
           return terSearch(A, d1, d2, x);
   } //end of the method

} //end of the class

Sample Output:

Searching for 100
100 element is found at index
location : 9
Searching for 500
500 is not found.


Related Solutions

Java String search Design and implement a recursive version of a binary search.  Instead of using a...
Java String search Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates can be entered by the user (that are...
Task 2: Recursive Binary Search Part A - Target in list? Write a function simple recursive...
Task 2: Recursive Binary Search Part A - Target in list? Write a function simple recursive binary search(lst, target) which returns True if target is in lst; otherwise False. You must use recursion to solve this problem (i.e. do not use any loops). Part B - Where is target? Write a function advanced recursive binary search(lst, target, lo, hi) which returns the index of an instance of target in lst between lo and hi, or None if target is not...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
Problem 3: (a) Implement a sublinear running time complexity recursive function in Java public static long...
Problem 3: (a) Implement a sublinear running time complexity recursive function in Java public static long exponentiation(long x, int n) to calculate x^n. Note: In your function you can use only the basic arithmetic operators (+, -, *, %, and /). (b) What is the running time complexity of your function? Justify. (c) Give a number of multiplications used by your function to calculate x^63. Important Notes: • For the item (a): o You must add the main method in...
implement this search function using CPP Coding language bool binarySearch(vector<int>vec,int low,int high, int search) Test your...
implement this search function using CPP Coding language bool binarySearch(vector<int>vec,int low,int high, int search) Test your code in main
For C++ Function 1: Write a recursive function to perform a sequential search on a set...
For C++ Function 1: Write a recursive function to perform a sequential search on a set of integers The function will require an array parameter and the number to look for. These are the minimal parameter requirements The function should take an array of any size Function 2: Write a recursive function that will convert an integer (base 10) to binary The function should only have an integer parameter Have the function write the binary number to the console You...
Language: Java Design and implement a program that implements an Interpolation Search method. Interpolation search is...
Language: Java Design and implement a program that implements an Interpolation Search method. Interpolation search is similar to binary search, except it tries to begin the search nearer to the location of the item. Instead of the using the middle value of the sorted array, interpolation search estimates the location of the target with respect to the first & last values in the array. The implementation is the same as binary search except that you should calculate the mid value...
Problem 1: Recursive anagram finder please used Java please Write a program that will take a...
Problem 1: Recursive anagram finder please used Java please Write a program that will take a word and print out every combination of letters in that word. For example, "sam" would print out sam, sma, asm, ams, mas, msa (the ordering doesn't matter) input: cram output: cram crma carm camr cmra cmar rcam rcma racm ramc rmca rmac acrm acmr arcm armc amcr amrc mcra mcar mrca mrac macr marc Hints: Your recursive function should have no return value and...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with comments and I need the input file to be placed with Scanner not BufferedReader Please help I need Class River Class CTRiver and Class Driver Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT