siteName > > searches.InterpolationSearch
Threads
java ( 1535646 ) - java ( 1535689 ) stack: com.thealgorithms.searches.InterpolationSearch.main(InterpolationSearch.java:53)
package com.thealgorithms.searches;

import java.util.Arrays;
import java.util.Random;
import java.util.stream.IntStream;

/**
 * Interpolation search algorithm implementation
 *
 * <p>
 * Worst-case performance O(n) Best-case performance O(1) Average performance
 * O(log(log(n))) if the elements are uniformly distributed if not O(n)
 * Worst-case space complexity O(1)
 *
 * @author Podshivalov Nikita (https://github.com/nikitap492)
 */
class InterpolationSearch {

    /**
     * @param array is a sorted array
     * @param key is a value what shoulb be found in the array
     * @return an index if the array contains the key unless -1
     */
    public int find(int[] array, int key) {
        // Find indexes of two corners
        int start = 0;
        int end = (array.length - 1);

        // Since array is sorted, an element present
        // in array must be in range defined by corner
        while (start <= end && key >= array[start] && key <= array[end]) {
            // Probing the position with keeping
            // uniform distribution in mind.
            int pos = start + (((end - start) / (array[end] - array[start])) * (key - array[start]));

            // Condition of target found
            if (array[pos] == key) {
                return pos;
            }

            // If key is larger, key is in upper part
            if (array[pos] < key) {
                start = pos + 1;
            } // If key is smaller, x is in lower part
            else {
                end = pos - 1;
            }
        }
        return -1;
    }

    // Driver method
    public static void main(String[] args) {
        Random r = new Random();
        int size = 100;
        int maxElement = 100000;
        int[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray();

        // the element that should be found
        int shouldBeFound = integers[r.nextInt(size - 1)];

        InterpolationSearch search = new InterpolationSearch();
        int atIndex = search.find(integers, shouldBeFound);

        System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);

        int toCheck = Arrays.binarySearch(integers, shouldBeFound);
        System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
    }
}
Variables All
No.FromNameValue
153args[Ljava.lang.String;@7852e922
END 0 00
Output All Filter Merge
Process FilterThread Filter
1535646 java 1535689 java
No.PNPIDTIDTNMessage
1java15356461535689javaShould be found:
2java15356461535689java8369
3java15356461535689java. Found
4java15356461535689java8369
5java15356461535689javaat index
6java15356461535689java4
7java15356461535689java. An array length
8java15356461535689java100
9java15356461535689java
10java15356461535689javaFound by system method at an index:
11java15356461535689java4
12java15356461535689java. Is equal:
13java15356461535689javatrue
14java15356461535689java
END 0 0 0 00
Project:Alg-Java
Update:20240824
Commit:a7cd97d7
Source Code:searches.InterpolationSearch
BuildTool:Java17
Compiler:Java17
Runtime:Openjdk17
System:MySystemD
Kernel:Linux5.10.211
Cpu:Intel:Corei7-7700K
Machine:AwesomeMachine