Find a summary of the algorithm (search order, binary search, interpolation search, Fibonacci search)

Fifth, find

1, the concept of

2, search algorithm

Common lookup algorithm sequential (linear) lookup , binary search , (binary) lookup , interpolation lookup , Fibonacci lookup

2.1 sequential search

Sequential search (Sequential Search) known as a linear search, is the most basic technology find its discovery process: the first (or last) record start from the table, one by one element in the sequence is compared with a given value, If the elements of a sequence and the given values ​​are equal, then the lookup is successful; if you know the last (or first) element, which elements are not equal to a given value and comparison, the table does not have the check element, the lookup unsuccessful.

  public static int sequenceSearch(int[] arr,int value){
        int temp=0;
        for(int i=0;i<arr.length;i++){
            if(arr[i]==value){
                temp=i;
                break;

            }else {
                temp= -1;
            }

        }
        return temp;
    }

2.4 Binary Search

Algorithm description (in sequence to ensure orderly)

  • First determining an intermediate index of the array

    mid=(left+right)/2
    
  • Then let the number find Val and arr need to find a [mid] Compare

    find>arr[mid],说明要查找的数在mid的右边,需要递归的向右查找。
    find<arr[mid],说明要查找的数在mid的左边,需要递归的向左查找。
    findVal==arr[mid]说明找到,就返回
    
  • When do we need to end the recursion

    找到就结束递归
    递归完整个数组,仍然没有找到findVal,也需要结束递归,当left>right就需要退出
    

A code implementation (find elements in the sequence, find a)

 public static int binarySearch(int[] arr, int left, int right, int value) {
        int mid = (right + left) / 2;
        if (left > right) {
            return -1;
        }
        if (value > arr[mid]) {
            //向右递归
            return binarySearch(arr, mid + 1, right, value);
        } else if (value < arr[mid]) {
            //向左递归
            return binarySearch(arr, left, mid - 1, value);
        } else {
            return mid;
        }
    }

2 code implements (Find all positions of the specified element in the sequence index)

 public static ArrayList<Integer> binarySearch2(int[] arr, int left, int right, int value) {
        int mid = (right + left) / 2;
        if (left > right) {
            return new ArrayList<>();
        }
        if (value > arr[mid]) {
            //向右递归
            return binarySearch2(arr, mid + 1, right, value);
        } else if (value < arr[mid]) {
            //向左递归
            return binarySearch2(arr, left, mid - 1, value);
        } else {
            ArrayList<Integer> list = new ArrayList<>();
            int temp = mid-1;
            //向左索引
            while (true) {
                if (temp< 0 ||arr[temp] != value) {
                    break;
                }
                list.add(temp);
                temp--;
            }
            //向右索引
            temp=mid+1;
            while (true) {
                if (temp > arr.length - 1 ||arr[temp] != value) {
                    break;
                }
                list.add(temp);
                temp++;
            }
            list.add(mid);
            return list;
        }
    }

2.5 Interpolation Find

Find a simple interpolation algorithm is not carried out from the middle, it is gradually carried out a search based on the values ​​we need to query that searches based on the distance and the evaluation of its money precondition to ensure an orderly sequence.

mid=left+(right-low)*(findValue-arr[left])/(arr[right]-arr[left])

Note:

  • For larger amounts of data, the data distribution is relatively uniform sequence, using interpolation to find, faster
  • Uneven distribution of data in the sequence, the interpolation is not bound to look good binary search
 public static int insertValueSearch(int[] arr,int left,int right,int findValue){
        if(left>right||findValue<arr[left]||findValue>arr[right]){
            return -1;
        }
        int mid=left+(right-left)*(findValue-arr[left])/(arr[right]-arr[left]);
        if(findValue>arr[mid]){
            //向右递归
           return insertValueSearch(arr,mid+1,right,findValue);
        }
      else if(findValue<arr[mid]){
            //向左递归
           return insertValueSearch(arr,left,mid-1,findValue);
        }
      else {
          return mid;
        }
    }

2.6 Fibonacci Find

Algorithm Description:

Fibonacci algorithm is based on the Fibonacci column-based (also known as the golden number of columns), the ratio after its former one and one with the increase in the amount of digital golden ratio 0.618 gradually approaching. So Fibonacci changed the binary search to find the way to solve any original value mid, which no longer represents the mid value, but represent a golden point.

1, if given the same value, then the lookup is successful, return position in the table.

2, if smaller than a given value, look to the right and to reduce the two Fibonacci space

3, if larger than a given value, and reduce the left to find a Fibonacci space

4, the process is repeated, until it finds a given value (success) or empty set interval (failed)

   //斐波那契算法查找
    public static int fibonacciSearch(int[] arr, int findValue) {
        int left = 0;
        int right = arr.length - 1;
        int mid = 0;
        int k = 0;//记录斐波那契分隔数值的下标
        int f[] = fib();//获得斐波那契数列

        while (right > f[k] - 1) {
            k++;
        }
        //对数组a中的元素数量与斐波那契f[k]值相等
        int[] temp = Arrays.copyOf(arr, f[k]);
        //将填充的元素等于原数组的最后一个元素,进行元素补充
        for (int i = right + 1; i < temp.length; i++) {
            temp[i] = temp[right];
        }

        //斐波那契查找
        while (left <= right) {
            mid = left + f[k - 1] - 1;
            if (findValue < temp[mid]) {
                //分割点左边的数据继续进行分割
                right = mid - 1;
                k--;
            } else if (findValue > temp[mid]) {
                //对分割点右边的数据继续进行分割
                left = mid + 1;
                k -= 2;
            } else {
                if (mid < right) {
                    return mid;
                } else {
                    return right;
                }

            }
        }
        return -1;
    }
发布了71 篇原创文章 · 获赞 42 · 访问量 6万+

Guess you like

Origin blog.csdn.net/dreame_life/article/details/104167886