Question

In: Computer Science

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

Solutions

Expert Solution

import java.util.Scanner;
import java.util.Arrays;
public class Main
{
public static int interpolationSearch(int arr[], int n, int key)
{
  
int low = 0, high = (n - 1);
while (low <= high && key >= arr[low] && key <= arr[high])

{
if (low == high)
{
  
if (arr[low] == key) return low;
return -1;
  

  
}
  
int position = low + (((high-low) / (arr[high]-arr[low]))*(key - arr[low]));

if (arr[position] == key)
{
return position;
}

else if (arr[position] < key)
{
low = position + 1;
}

else if(arr[position] > key)
{
high = position - 1;
}
}
  
return -1;
}
public static void main( String[] args)
{
  
Scanner scan = new Scanner(System.in);
int a[] = new int[100];
  
System.out.println("Enter number of elements :");
  
int n = scan.nextInt();
  
int i,index;
  
for(i=0;i<n;i++)
{
a[i] = scan.nextInt();
}
  
System.out.println("Enter key to be search :");

int key = scan.nextInt();
  
Arrays.sort(a,0,n-1);
index=interpolationSearch(a,n,key);
  
if(index!=-1)
{
System.out.println("Element is present in array at"+index);
}
  
else
System.out.println("Element is not present in array");
}
}


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...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement the compareTo method to compare the triangles on the basis of similarity. Draw the UML diagram for your classes. Write a Driver class (class which instantiates the comparableTriangle objects) to determine if two instances of ComparableTriangle objects are similar (sample output below). It should prompt the user to enter the 3 sides of each triangle and then display whether or not the are similar...
Write a Java program that implements the Depth-First Search (DFS) algorithm. Input format: This is a...
Write a Java program that implements the Depth-First Search (DFS) algorithm. Input format: This is a sample input from a user. 3 2 0 1 1 2 The first line (= 3 in the example) indicates that there are three vertices in the graph. You can assume that the first vertex starts from the number 0. The second line (= 2 in the example) represents the number of edges, and following two lines are the edge information. This is the...
Language is Java Design and write a Java console program to estimate the number of syllables...
Language is Java Design and write a Java console program to estimate the number of syllables in an English word. Assume that the number of syllables is determined by vowels as follows. Each sequence of adjacent vowels (a, e, i, o, u, or y), except for a terminal e, is a syllable. However, the minimum number of syllables in an English word is one. The program should prompt for a word and respond with the estimated number of syllables in...
Design and implement a Java program that creates a GUI that will allow a customer to...
Design and implement a Java program that creates a GUI that will allow a customer to order pizza and other items from a Pizza Paarlor. The customer should be able to order a variety of items which are listed below. The GUI should allow the customer (viaJavaFX UI Controls - text areas, buttons, checkbox, radio button, etc.) to input the following information: Name of the customer First Name Last Name Phone number of the customer Type of food being order...
programing language JAVA: Design and implement an application that reads a sentence from the user, then...
programing language JAVA: Design and implement an application that reads a sentence from the user, then counts all the vowels(a, e, i, o, u) in the entire sentence, and prints the number of vowels in the sentence. vowels may be upercase
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads...
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads in the principle 2) reads in additional money deposited each year (treat this as a constant) 3) reads in years to grow, and 4) reads in interest rate And then finally prints out how much money they would have each year. See below for formatting. Enter the principle: XX Enter the annual addition: XX Enter the number of years to grow: XX Enter the...
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display...
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display the area and perimeter of a rectangle with width = 4 and height = 8.
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to keep track of a student's current grade. You also get to design and write WeightedCourseGradeDriver class that requests input from the user and interacts with the WeightedCourseGrade class. Your WeightedCourseGrade class should store the following information: Weighted subtotal (the sum of all of the categories multiplied by the grade category weight) Total category weights (the sum of all the grade category weights) Provide the...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT