Java Program to Implement Interpolation Search Algorithm

This is a Java Program to Implement Interpolation Search Algorithm. Interpolation search (sometimes referred to as extrapolation search) is an algorithm for searching for a given key value in an indexed array that has been ordered by the values of the key. On average the interpolation search makes about log(log(n)) comparisons (if the elements are uniformly distributed), where n is the number of elements to be searched. In the worst case (for instance where the numerical values of the keys increase exponentially) it can make up to O(n) comparisons.
In interpolation-sequential search, interpolation is used to find an item near the one being searched for, then linear search is used to find the exact item.

Here is the source code of the Java Program to Implement Interpolation Search Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/**
 ** Java Program to Implement Interpolation Search Algorithm
 **/
 
import java.util.Scanner;
 
/** Class InterpolationSearch **/
public class InterpolationSearch
{
    /** interpolationSearch function **/
    public static int interpolationSearch(int[] sortedArray, int toFind)
    {
        int low = 0;
        int high = sortedArray.length - 1;
        int mid;
        while (sortedArray[low] <= toFind && sortedArray[high] >= toFind) 
        {
            if (sortedArray[high] - sortedArray[low] == 0)
                return (low + high)/2;
            /** out of range is possible  here **/
             mid = low + ((toFind - sortedArray[low]) * (high - low)) / (sortedArray[high] - sortedArray[low]);  
 
             if (sortedArray[mid] < toFind)
                 low = mid + 1;
             else if (sortedArray[mid] > toFind)
                 high = mid - 1;
             else
                 return mid;
        }
        if (sortedArray[low] == toFind)
            return low;
           /** not found **/
        else
            return -1; 
    }    
    /** Main method **/
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner( System.in );        
        System.out.println("Interpolation Search Test\n");
        int n, i;
        /** Accept number of elements **/
        System.out.println("Enter number of integer elements");
        n = scan.nextInt();
        /** Create integer array on n elements **/
        int arr[] = new int[ n ];
        /** Accept elements **/
        System.out.println("\nEnter "+ n +" sorted integer elements");
        for (i = 0; i < n; i++)
            arr[i] = scan.nextInt();
        System.out.println("\nEnter element to search for : ");
        int key = scan.nextInt();
 
        int result = interpolationSearch(arr, key);
 
        if (result == -1)
            System.out.println("\n"+ key +" element not found");
        else
            System.out.println("\n"+ key +" elemnt found at position "+ result);
 
    }    
}
Interpolation Search Test
 
Enter number of integer elements
10
 
Enter 10 sorted integer elements
12 24 36 48 60 72 84 96 108 120
 
Enter element to search for :
24
 
24 elemnt found at position 1

Related posts:

Hashing a Password in Java
Java Program to Implement the Checksum Method for Small String Messages and Detect
Hướng dẫn Java Design Pattern – MVC
Format ZonedDateTime to String
Java Program to Compute Determinant of a Matrix
The Modulo Operator in Java
Hướng dẫn sử dụng Printing Service trong Java
Hướng dẫn Java Design Pattern – Composite
LinkedHashSet trong Java hoạt động như thế nào?
Setting Up Swagger 2 with a Spring REST API
Spring Data MongoDB – Indexes, Annotations and Converters
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Hướng dẫn Java Design Pattern – Interpreter
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Implement Regular Falsi Algorithm
HttpClient 4 Cookbook
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Java Program to Implement the Vigenere Cypher
Phương thức forEach() trong java 8
Java Program to Represent Graph Using 2D Arrays
Java Program to Implement ConcurrentSkipListMap API
Quản lý bộ nhớ trong Java với Heap Space vs Stack
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Java Program to Implement PriorityQueue API
Error Handling for REST with Spring
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Mệnh đề if-else trong java
New Features in Java 13
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
The Guide to RestTemplate
Java Program to Implement Bloom Filter
Giới thiệu Google Guice – Dependency injection (DI) framework