Question

I'm writing a program in java that performs an interpolation search on an array of type...

I'm writing a program in java that performs an interpolation search on an array of type double. I need a method that takes in that double array and a value to be searched for that is also of type double. I can find an implementation of the interpolation search that works with integer arrays but not with type double arrays. I need a method interpolationSearch(double[] arr, double searchValue) that returns the postion of where the value is if found and a -1 if not found in the array. I also need it to return the amount of steps it took to find the value using a counter everytime the interpolation search splits the array. Any help is appreciated and I will gladly give a thumbs up to correct answers!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

class Main
{
public static int interpolation_search(double array[],double searchValue)
{
int n=array.length;
   int low = 0, high = (n - 1);
   while (low <= high && searchValue >= array[low] && searchValue <= array[high])
   {
       if (low == high)
       {
           if (array[low] == searchValue)
           return low;
           return -1;
       }
       int position = low + (int)(((double)(high - low) / (array[high] - array[low])) * (searchValue - array[low]));
       if (array[position] == searchValue)
           return position;
       if (array[position] < searchValue)
           low = position + 1;
       else
           high = position - 1;
   }
   return -1;
}
public static void main(String args[])
{
   double array[] = {10.4, 12.45, 13.67, 16.78, 18.99, 19.991, 20.670, 21.55,22.78, 23.70, 24.122, 33.234, 35.455, 42.67, 47.888};
   int n = array.length;
   double searchValue = 12.45;
   int index = interpolation_search(array,searchValue);
   if (index != -1)
       System.out.println("Element found at index "+index);
   else
       System.out.println("Element not found.");
}
}

OUTPUT:

Element found at index 1


Add a comment
Know the answer?
Add Answer to:
I'm writing a program in java that performs an interpolation search on an array of type...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design and implement a program that implements an Interpolation Search method. Interpolation search is similar to...

    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 as: mid...

  • Need help with this Java project implementing an interpolation search, bolded the missing parts: /**   ...

    Need help with this Java project implementing an interpolation search, bolded the missing parts: /**    A class that implements an interpolation search and    a binary search of a sorted array of integers.    @author Frank M. Carrano    @author Timothy M. Henry    @version 4.0 */ public class SearchComparer {    private int[] a;    private int interpolationSearchCounter;    private int binarySearchCounter;    private static final int CAPACITY = 50;    public SearchComparer(int[] array, int n)    {...

  • Consider an ordered array A of size n and the following ternary search algorithm for finding...

    Consider an ordered array A of size n and the following ternary search algorithm for finding the index i such that A[i] = K. Divide the array into three parts. If A[n/3] > K. the first third of the array is searched recursively, else if A[2n/3] > K then the middle part of the array is searched recursively, else the last thud of the array is searched recursively. Provisions are also made in the algorithm to return n/3 if A[n/3]...

  • Using Java write a program that performs the following: 1. Define a class that contains an...

    Using Java write a program that performs the following: 1. Define a class that contains an array of type int as a data member. 2. Define a constructor that creates an array of 1024 elements and assigns it to the class data member. The constructor shall populate the elements with random numbers ranging in value from 1 to 1024. 3. Define and implement a search() method that take a parameter of type int and returns the index location if the...

  • write a program which include a class containing an array of words (strings).The program will search...

    write a program which include a class containing an array of words (strings).The program will search the array for a specific word. if it finds the word:it will return a true value.if the array does not contain the word. it will return a false value. enhancements: make the array off words dynamic, so that the use is prompter to enter the list of words. make the word searcher for dynamic, so that a different word can be searched for each...

  • 3. Write Java methods to accomplish each of the following Write a method named simpleAry which...

    3. Write Java methods to accomplish each of the following Write a method named simpleAry which accepts and return nothing. It creates an array that can hold ten integers. Fill up each slot of the array with the number 113. Then, display the contents of the array on the screen. You must use a loop to put the values in the array and also to display them. Write a method named sury which accepts an array of type double with...

  • Question 1) Suppose a program has the following code: const int X = 2, Y =...

    Question 1) Suppose a program has the following code: const int X = 2, Y = 3; int sum; int values[X][Y] = {{1, 2, 3},                                  {4, 5, 6}}; for(int i=0; i<X; i++) {      sum=0;      for(int j=0; j<Y; j++)         sum+=values[i][j];    cout<<sum<<endl; } What is this program getting the sum of? Group of answer choices D-) The columns of the 2D array C-) The rows of the 2D array A-) All of the elements of the 2D...

  • I'm writing a program in java called Sokoban. I'm pretty far in, but there are a...

    I'm writing a program in java called Sokoban. I'm pretty far in, but there are a few methods that I have no idea where to go from where I am now. Method 1: /**    * Moves a box on the board.    *    * Step 1: Use your checkDelta method to check that the move is valid. Recall that there are 2    * characters that can represent a box. Step 2: Use your togglePos method to correctly...

  • I should use the array and loop to create a java program according to the instruction,...

    I should use the array and loop to create a java program according to the instruction, but I have no idea how to do it. Introduction This lab assignment continues to give you practice using loops, particularly loops with variable termination conditions, and it also provides you an opportunity to use one-dimensional arrays. Recall that an array is used to store a collection of data. The data can be values of Java primitive data types or else objects (for instance,...

  • Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods...

    Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods are meant to determine if an array of objects corresponds to a heap. The methods are generic, and they should work with an array of any type T that implement Comparable<T>. Implement the second method so that it uses recursion to process the complete tree/subtree whose root is at position i in the array arr. The method should return true if that tree/subtree is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT