查找算法

线性查找、二分查找和插值查找

public class Search {

    public static void main(String[] args) {
        int[] array = {1,3,100,100,300};
        System.out.println(seqSearch(array,100));
        System.out.println(binarySearch(array, 0, array.length - 1,100));
        System.out.println(insertValueSearch(array, 0, array.length - 1,100));
    }

    /**
     * 线性查找
     * @param array
     * @param findValue
     * @return
     */
    public static List<Integer> seqSearch(int[] array, int findValue) {
        List<Integer> indexs = new ArrayList<>();
        for (int i = 0;i < array.length; i++) {
            if (findValue ==  array[i]) {
                indexs.add(i);
            }
        }
        return indexs;
    }

    /**
     * 二分查找:前提 数列为有序数列
     * @param array
     * @param left
     * @param right
     * @param findValue
     * @return
     */
    public static List<Integer> binarySearch(int[] array,int left, int right, int findValue) {
        List<Integer> indexs = new ArrayList<>();
        if (left > right) {
            return indexs;
        }
        int mid = (left + right) / 2;
        int midValue = array[mid];
        if (findValue  > midValue) {
            return binarySearch(array, mid + 1, right, findValue);
        } else if (findValue < midValue) {
            return binarySearch(array, left, mid - 1, findValue);
        } else {
            return getList(indexs,findValue,mid, array);
        }
    }


    /**
     * 插值查找: 前提 数列为有序数列
     * @param array
     * @param left
     * @param right
     * @param findValue
     * @return
     */
    public static List<Integer> insertValueSearch(int[] array,int left, int right, int findValue) {
        List<Integer> indexs = new ArrayList<>();
        if (left > right || findValue < array[left] || findValue > array[right]) {
            return indexs;
        }
        int mid = left + (right - left) * (findValue - array[left]) / (array[right] - array[left]);
        int midValue = array[mid];
        if (findValue > midValue) {
            return insertValueSearch(array, mid + 1, right, findValue);
        } else if (findValue < midValue) {
            return insertValueSearch(array, left, mid - 1, findValue);
        } else {
            return getList(indexs,findValue,mid, array);
        }
    }

    public static List<Integer> getList(List indexs, int findValue, int mid,int[] array) {
        indexs.add(mid);
        int temp = mid - 1;
        while (true) {
            if (temp < 0 ||  array[temp] != findValue ) {
                break;
            }
            indexs.add(temp);
            temp--;
        }

        temp = mid + 1;
        while (true) {
            if (temp > array.length - 1 || array[temp] != findValue) {
                break;
            }
            indexs.add(temp);
            temp++;
        }

        return indexs;
    }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值