查找算法总结

这篇博客详细介绍了四种查找算法的Java实现,包括顺序查找、二分查找(迭代和递归两种方式)、差值查找(自适应划分)以及斐波那契查找。这些算法在不同场景下具有不同的效率和适用性,对于理解数组搜索和优化查找过程有很好的帮助。
摘要由CSDN通过智能技术生成

查找算法总结

1、顺序查找

package com.namu.search;

public class SeqSearch {

    public static int[] array = new int[]{1,2,3,7,10,17};

    public static int target = 17;

    public static void main(String[] args) {
        System.out.println(array[seqSearch1(array,target)]);
    }

    public static int seqSearch1(int[] array, int target) {
        for (int i = 0 ; i < array.length ; i++) {
            if (array[i] == target) {
                return i;
            }
        }
        return -1;
    }
}

2、二分查找

package com.namu.search;

public class BinarySearch {

    public static int[] array = new int[]{1,2,3,7,10,17};

    public static int target = 17;

    public static void main(String[] args) {
        //System.out.println(array[binarySearch2(array,0,array.length-1,target)]);
        System.out.println(array[binarySearch1(array,target)]);
    }

    /**
     * @methodName: binarySearch1
     * @description: 二分查找迭代法
     * @author: namusangga
     * @date: 2022/10/17 11:04
     * @param: array
     * @param: target
     * @return: int
     * @throws:
     */
    public static int binarySearch1(int[] array, int target) {
        int low = 0;
        int high = array.length-1;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (target < array[mid]) {
                high = mid - 1;
            }
            if (target > array[mid]) {
                low = mid + 1;
            }
            if (target == array[mid]) {
                return mid;
            }
        }
        return -1;
    }

    /**
     * @methodName: binarySearch2
     * @description: 二分查找递归法
     * @author: namusangga
     * @date: 2022/10/17 11:05
     * @param: array
     * @param: low
     * @param: high
     * @param: target
     * @return: int
     * @throws:
     */
    public static int binarySearch2(int[] array, int low, int high, int target) {
        if (low > high) {
            return -1;
        }
        int mid = (low + high) / 2;
        if (target < array[mid]) return binarySearch2(array, low, mid-1, target);
        if (target > array[mid]) return binarySearch2(array, mid+1, high, target);
        if (target == array[mid]) return mid;
        return -1;
    }
}


3、差值查找(自适应的划分,适合平均分布的数组)

package com.namu.search;

public class InsertValueSearch {
    public static int[] array = new int[]{1,2,3,7,10,17};

    public static int target = 17;

    public static void main(String[] args) {
        System.out.println(array[insertValueSearch2(array,0,array.length-1,target)]);
        //System.out.println(array[insertValueSearch1(array,target)]);
    }

    /**
     * @methodName: insertValueSearch1
     * @description: 迭代法
     * @author: namusangga
     * @date: 2022/10/17 13:37
     * @param: array
     * @param: target
     * @return: int
     * @throws:
     */
    public static int insertValueSearch1(int[] array, int target) {
        int low = 0;
        int high = array.length-1;
        while (low <= high) {
            int mid = low + (target-array[low])/(array[high]-array[low])*(high-low);
            if (target < array[mid]) {
                high = mid-1;
            }
            if (target > array[mid]) {
                low = mid+1;
            }
            if (target == array[mid]) {
                return mid;
            }
        }
        return -1;
    }

    /**
     * @methodName: insertValueSearch2
     * @description: 递归法
     * @author: namusangga
     * @date: 2022/10/17 13:40 
     * @param: array
     * @param: low
     * @param: high
     * @param: target 
     * @return: int
     * @throws: 
     */
    public static int insertValueSearch2(int[] array, int low , int high , int target) {
        if (low > high) {
            return -1;
        }
        int mid = low + (target-array[low])/(array[high]-array[low])*(high-low);
        if (target < array[mid]) return insertValueSearch2(array, low, mid-1, target);
        if (target > array[mid]) return insertValueSearch2(array, low+1, high, target);
        if (target == array[mid]) return mid;
        return -1;
    }
}

4、斐波那契查找

package com.namu.search;

import java.util.Arrays;

public class FibonacciSearch {

    public static int[] array = new int[]{1, 2, 3, 7, 10, 17};

    public static int target = 17;

    public static void main(String[] args) {
        System.out.println(array[fibonacciSearch1(array,target)]);
    }

    /**
     * @methodName: fib
     * @description: 构造斐波那契数组
     * @author: namusangga
     * @date: 2022/10/17 14:03
     * @return: int[]
     * @throws:
     */
    public static int[] fib() {
        int[] f = new int[20];
        f[0] = 1;
        f[1] = 1;
        for (int i = 2 ; i < 20 ; i++) {
            f[i] = f[i-1] + f[i-2];
        }
        return f;
    }

    public static int fibonacciSearch1(int[] array, int target) {
        int low = 0;
        int high = array.length-1;
        int[] fib = fib();
        int f = 0;
        while (high > fib[f]-1) {
            f++;
        }
        int[] newArray = Arrays.copyOf(array, fib[f]);
        for (int i = high+1 ; i < newArray.length ; i++) {
            newArray[i] = array[high];
        }
        //开始查找
        while (low <= high) {
            int mid = low + fib[f-1] - 1;
            if (target < newArray[mid]) {
                high = mid-1;
                f-=1;
            }
            if (target > newArray[mid]) {
                low = mid+1;
                f-=2;
            }
            if (target == newArray[mid]) {
                if (mid >= high) {
                    return high;
                }else {
                    return mid;
                }
            }
        }
        return -1;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值