/
NikolayIvkin
/
TheAlgorithms
Обзор
Документация
Войти
/
NikolayIvkin
/
TheAlgorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/com/thealgorithms/searches/InterpolationSearch.java
57 строк
2 KB
Hardik Pawar
Add tests, remove `main`, improve docs in `InterpolationSearch` (#5666)
10 окт 2024, 22:36
Не верифицирован
10 окт 2024, 22:36
ba3d3bf
Код
Авторство
О чём код?
package com.thealgorithms.searches; /** * InterpolationSearch is an algorithm that searches for a target value within a sorted array * by estimating the position based on the values at the corners of the current search range. * * <p> * The performance of this algorithm can vary: * - Worst-case performance: O(n) * - Best-case performance: O(1) * - Average performance: O(log(log(n))) if the elements are uniformly distributed; otherwise O(n) * - Worst-case space complexity: O(1) * </p> * * <p> * This search algorithm requires the input array to be sorted. * </p> * * @author Podshivalov Nikita (https://github.com/nikitap492) */ class InterpolationSearch { /** * Finds the index of the specified key in a sorted array using interpolation search. * * @param array The sorted array to search. * @param key The value to search for. * @return The index of the key if found, otherwise -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; } }