Interpolation search algorithm principle and code implementation

7.4 Interpolation search

7.4.1 Disadvantages of binary search algorithm

​ Each time you need to compare the data to be found with the middle element of the array, and then search the left and right parts. Of course, compared to sequential search, when the amount of data is large, half search has higher efficiency. Of course, it is inevitable that when extreme situations occur, binary search still requires multiple comparison searches. For example, for an array to be searched that is located at the far left or right of the entire ordered array, the binary search method still needs to perform comparison searches from the middle.

7.4.2 Introduction to interpolation search algorithm
  • The interpolation search algorithm is similar to binary search. The difference is that the interpolation search starts from the adaptive mid each time; (adaptive means that there is a relationship between the value of mid and the value to be searched, and the mid value changes adaptively, so that Get closer to the value you are looking for more quickly)

  • In binary search, the evaluation method of mid is:

Insert image description here

In the binary search, modify the formula for finding the mid index to:

Insert image description here

7.4.3 Code implementation
package com.kevin.search;

/**
 * @author : kevin ding
 * @date : 2022/3/11 22:52
 * @description : 插值查找算法
 */
public class InsertSearchDemo {
    
    
    public static void main(String[] args) {
    
    
        int[] array = {
    
    11, 12, 33, 37, 42, 75, 88, 174, 189, 325, 456};
        int targetValue = 11;
        int valueIndex= insertValueSearch(array, 0, array.length - 1, targetValue);
        if(valueIndex == -1){
    
    
            System.out.println("没有找到....");
        }else {
    
    
            System.out.println("找到了," + targetValue + "在array中的索引位置为:" + valueIndex);
        }

    }

    public static int insertValueSearch(int[] array, int left, int right, int targetVal){
    
    
        System.out.println("查找次数..");
        // 判断是否越界
        if(left > right || targetVal < array[0] || targetVal > array[array.length-1]){
    
    
            return -1;
        }
        // mid的值为自适应值
        int mid = left + (right - left) * (targetVal - array[left]) / (array[right] - array[left]);
        int midValue = array[mid];

        if(targetVal > midValue){
    
    
            // 向右递归查找
            return insertValueSearch(array, mid+1, right, targetVal);
        }else if (targetVal < midValue){
    
    
            // 向左递归查找
            return insertValueSearch(array, left, mid-1, targetVal);
        }else {
    
    
            // 找到了位置,mid
            return mid;
        }


    }
}
7.4.4 Comparison of results

When searching for 11 in the arrays {11, 12, 33, 37, 42, 75, 88, 174, 189, 325, 456} respectively in the program, the number of binary searches and the results are: it took a total of 3 searches to find:

Insert image description here

The result of the interpolation search algorithm is that it was found after only one search:

Insert image description here
Summarize

  • For lookup tables with large amounts of data and relatively even distribution of keywords, interpolation is used to search faster.
    -When the elements are unevenly distributed, the interpolation search method is not necessarily better than the half search method.

Guess you like

Origin blog.csdn.net/weixin_43155804/article/details/123436081