Question

In: Computer Science

Problem 2: There are no data errors that need to be checked as all the data...

  • Problem 2:

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

  • You can use the array of the previous example to test your program, however, I

suggest that you also use other input arrays to validate the correctness and efficiency of your solution.

• Your program MUST be submitted only in source code form (.java file).

Write a program in Java to implement a recursive search functionint 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.

  • Your program MUST be submitted only in source code form (.java file).

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

Solutions

Expert Solution

For ternary search, everything remains the same as binary search, except two mid values are calculated according to the formula and increased checking conditions to update range of array to be searched. Range limits updation is explained in the program.

import java.util.*;

class Main
{
   // recursive function
   static int terSearch(int A[], int l, int r, int x) {
       if(l>r) // base condition, x not found
           return -1;
          
       // d1 and d2 calculation
       int d1 = l + (r-l)/3;
       int d2 = d1 + (r-l)/3;
      
       // decreasing the limits of array according to conditions
       if(x<A[d1]) {
           return terSearch(A, l, d1-1, x); // r=d1-1
       } else if(x==A[d1]){
           return d1;   // element at d1
       } else if(x>A[d1] && x<A[d2]) {
           return terSearch(A, d1+1, d2-1, x); //l=d1+1, r=d2-1
       } else if(x==A[d2]) {
           return d2;   // element at d2
       } else { // x>A[d2]
           return terSearch(A, d2+1, r, x);   //l=d2-1
       }
   }
  
   public static void main (String[] args)
   {
       Scanner sc = new Scanner(System.in);
       int n, x, foundIndex;
       System.out.println("Enter array size");
       n = sc.nextInt();   // array size input
       int A[] = new int[n];
       System.out.println("Enter array elements");
       for(int i=0;i<n;i++) {
           A[i] = sc.nextInt();   // array elements input
       }
       System.out.println("Enter element to be searched");
       x = sc.nextInt();
       foundIndex = terSearch(A,0,n-1,x);   // call the ternary search function
       if(foundIndex==-1)   // x not found
           System.out.println(x + " not found");
       else
           System.out.println(x + " found at index(starting from 0): "+foundIndex);
   }
}

Sample Code Run:


Related Solutions

5050 people are randomly selected and the accuracy of their wristwatches is checked, with positive errors...
5050 people are randomly selected and the accuracy of their wristwatches is checked, with positive errors representing watches that are ahead of the correct time and negative errors representing watches that are behind the correct time. The 5050 values have a mean of 111111sec and a population standard deviation of 233233sec. Use a 0.010.01 significance level to test the claim that the population of all watches has a mean of 00sec. The test statistic is The P-Value is The final...
45 people are randomly selected and the accuracy of their wristwatches is checked, with positive errors...
45 people are randomly selected and the accuracy of their wristwatches is checked, with positive errors representing watches that are ahead of the correct time and negative errors representing watches that are behind the correct time. The 45 values have a mean of 96sec. Assume a population standard deviation of 232sec. Use a 0.02 significance level to test the claim that the population of all watches has a mean of 0sec (use a two-sided alternative).
Correct all errors in the provided script. Remember that some errors are fatal (result in an...
Correct all errors in the provided script. Remember that some errors are fatal (result in an error message) while others simply cause the program to produce incorrect results. You can assume that you have fixed all errors when the checker passes all tests. %*************************************************************************************** %It is recommended that you debug this program offline and submit only once you have corrected the errors %*************************************************************************************** %% %These 3 loops all calculate the sum of the integers from 1 through 1000 for number...
Does the data contain errors? If so, write queries in SQL to find these errors and...
Does the data contain errors? If so, write queries in SQL to find these errors and propose a way to address the issues Theres a change of weight error (end weight-start weight) calculated wrong and a logical error
Districting allows for which of the following? (multiple answers are allowed - all must be checked...
Districting allows for which of the following? (multiple answers are allowed - all must be checked to be correct) Residents are placed in areas drawn on a map to separate them by race, economics and partisan preference. To make sure that every person is granted the right of representation in government. Residents may be placed within areas drawn on a map that shows who they vote for in elections. To allow for a mathematically ideal proportionment of populations so that...
1. How are Sampling Errors and Nonsampling Errors different? 2. What types of errors do you...
1. How are Sampling Errors and Nonsampling Errors different? 2. What types of errors do you feel are most critical? (It needs to be about 2 paragraphs)
We will use a data set in the “fpp” package for this question. You need to undertake all initial logistics as shown in class to do this problem.
  We will use a data set in the “fpp” package for this question. You need to undertake all initial logistics as shown in class to do this problem. (i. e install FPP package. Then call it to script file using library command) Data set = “fuel” : Fuel economy data on 2009 vehicles in the US.   Obtain the scatter plot between “Carbon” and “Highway” variables. Name x-axis as “Highway” and y-axis as “Carbon”. Fit the least square regression...
Using the data of UAS accidents decide if procedural/skill errors are = to other errors, what...
Using the data of UAS accidents decide if procedural/skill errors are = to other errors, what statistical test would you intend to use. Analyze your data to ensure that it meets the assumptions of the test (for example parametric vs. non-parametric). Table 1 UAS Accidents UAS Type Other Procedural/Skill Hunter 15 14 Shadow 10 4 Predator 7 7 Global Hawk 2 1
What is the meaning of two conditions of EUROCODE 2 which have to be checked in...
What is the meaning of two conditions of EUROCODE 2 which have to be checked in dimensioning of RC beam for shear? Indicate these elements on a sketch.
Problem 1. ( JUST NEED PROBLEM 2 SOLVED) Home’s demand curve for wheat is D =...
Problem 1. ( JUST NEED PROBLEM 2 SOLVED) Home’s demand curve for wheat is D = 100-20P. Its supply curve is. S = 20 + 20P. Suppose that Home is a small country. a. Graph the demand and supply curves. In the absence of trade, what would the price of wheat be in Home? The price of wheat in the world market is 1.5. What is the free-trade equilibrium price of wheat in Home? Determine the amount of domestic production,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT