Question

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 = first + ((last - first) * (searchKey – arr[first])) / (arr[last] - arr[first])


Note that Interpolation Search will only work with numeric types! Your interpolation
search method should work on an array of integers

Language;java

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

// Java program to implement interpolation search

class Test
{
   // Array of items on which search will
   // be conducted.
   static int arr[] = new int[]{10, 12, 13, 16, 18, 19, 20, 21, 22, 23,
                                       24, 33, 35, 42, 47};
  
   // If x is present in arr[0..n-1], then returns
   // index of it, else returns -1.
   static int interpolationSearch(int searchkey)
   {
       // Find indexes of two corners
       int first = 0, last = (arr.length - 1);
  
       // Since array is sorted, an element present
       // in array must be in range defined by corner
       while (first <= last && searchkey >= arr[first] && searchkey <= arr[last])
       {         

           if (first == last)
           {
               if (arr[first] == searchkey) return first;
               return -1;
           }
      
           // Probing the position with keeping
           // uniform distribution in mind.
          
           int pos = first + ((last-first)*(searchkey - arr[first])) /
               (arr[last]-arr[first]);
  
           // Condition of target found
           if (arr[pos] == searchkey)
               return pos;
  
           // If x is larger, x is in upper part
           if (arr[pos] < searchkey)
               first= pos + 1;
  
           // If x is smaller, x is in the lower part
           else
               last = pos - 1;
       }
       return -1;
   }
  
   // Driver method
   public static void main(String[] args)
   {
  
  
       int searchkey = 18; // Element to be searched
       int index = interpolationSearch(searchkey);
      
       // If element was found
       if (index != -1)
           System.out.println("Element found at index " + index);
           else
           System.out.println("Element not found.");
  
   }
}

Add a comment
Know the answer?
Add Answer to:
Design and implement a program that implements an Interpolation Search method. Interpolation search is similar to...
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
  • Program with generic merge sort and binary search method help. The programming language I'm using is...

    Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...

  • 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...

  • in JAVA program.Use top-down design to design and implement a program to ask the user to...

    in JAVA program.Use top-down design to design and implement a program to ask the user to enter a list of integers, and then display to the user the mean and median of this list. You should first prompt the user to specify the length of the list of integers. For this assignment, your code should create an array of size 10, and then allow the user to specify the number of integers in their list, up to a maximum of...

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

    (a) 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...

  • in JAVA please thanks. (need answer in a few hours !!) Exercise #1: Design and implement...

    in JAVA please thanks. (need answer in a few hours !!) Exercise #1: Design and implement a program (name it LinearBinarySearch) to implement and test the linear and binary search algorithm discussed in the lecture slides. Define method LinearSearch() to implement linear search of an array of integers. Modify the algorithm implementation to count number of comparisons it takes to find a target value (if exist) in the array. Define method BinarySearch() to implement binary search of an array of...

  • In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program...

    In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides. Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count...

  • 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)    {...

  • In C++, make the following binary search function work on an array of strings to give...

    In C++, make the following binary search function work on an array of strings to give you the index location of the string you want in the array. It currently works on integers only. int binarySearch(int arr[], int firstIndex, int lastIndex, int target){ int index; if (firstIndex>lastIndex) index = -1; else { int mid = firstIndex + (lastIndex - firstIndex) / 2; if (target == arr[mid]) index = mid; else if (target < arr[mid]) index = binarySearch(arr, firstIndex, mid -...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • Qi. Create a java project Question that includes: • A bubble sort method to sort your...

    Qi. Create a java project Question that includes: • A bubble sort method to sort your arrays. Implement the below recursive binary search. • A java main class where you: o Create an array of characters that contains the letters of your first name followed by your last name, without spaces. o Call the sorting method to sort the array o Call binary search method to find the position of the first letter 'a' in your array. // Recursive Binary...

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