• 040 查找算法


    1、

    java:

    1)(线)

    2)/

    3)

    4)

    2 、线

    {1,8,10,89,1000,1234}:

    package com.petrel.search;
    
    public class  SeqSearch {
        public static void main(String[] args) {
            int arr[]={1,9,11,-1,34,89};
    
            int value = seqSearch(arr,9);
            if(value == -1){
                System.out.println("没有找到值");
            }else {
                System.out.println("找到,下标为:"+value);
            }
        }
    
        private static int  seqSearch(int[]arr,int value){
            for (int i = 0;i< arr.length;i++){
                if(arr[i] == value){
                    return i;
                }
            }
            return -1;
        }
    }
    
    结果是:
    D:jdk1_8jdkinjava.exe "-javaagent:D:soft_installideaIntelliJ IDEA 2020.1.3libidea_rt.jar=13420:D:soft_installideaIntelliJ IDEA 2020.1.3in" -Dfile.encoding=UTF-8 -classpath D:jdk1_8jdkjrelibcharsets.jar;D:jdk1_8jdkjrelibdeploy.jar;D:jdk1_8jdkjrelibextaccess-bridge.jar;D:jdk1_8jdkjrelibextcldrdata.jar;D:jdk1_8jdkjrelibextdnsns.jar;D:jdk1_8jdkjrelibextjaccess.jar;D:jdk1_8jdkjrelibextjfxrt.jar;D:jdk1_8jdkjrelibextlocaledata.jar;D:jdk1_8jdkjrelibext
    ashorn.jar;D:jdk1_8jdkjrelibextsunec.jar;D:jdk1_8jdkjrelibextsunjce_provider.jar;D:jdk1_8jdkjrelibextsunmscapi.jar;D:jdk1_8jdkjrelibextsunpkcs11.jar;D:jdk1_8jdkjrelibextzipfs.jar;D:jdk1_8jdkjrelibjavaws.jar;D:jdk1_8jdkjrelibjce.jar;D:jdk1_8jdkjrelibjfr.jar;D:jdk1_8jdkjrelibjfxswt.jar;D:jdk1_8jdkjrelibjsse.jar;D:jdk1_8jdkjrelibmanagement-agent.jar;D:jdk1_8jdkjrelibplugin.jar;D:jdk1_8jdkjrelib
    esources.jar;D:jdk1_8jdkjrelib
    t.jar;D:2_my_codejavadataStructureoutproductiondataStructure com.petrel.search.SeqSearch
    找到,下标为:1
    
    Process finished with exit code 0
    

    3、二分查找算法

    1){1,8,10,89,1000,1234}""

    2)

     3)码:

    :{1,8,10,89,1000,10001234}1000.

    package com.petrel.BinarySearch;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class BinarySearch {
        public static void main(String[] args) {
            int arr[] = { 1, 8, 10, 89,1000,1000, 1234 };
            int index = binarySearch(arr,1000);
            System.out.println("index:"+index);
    
            int arr1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 , 11, 12, 13,14,15,16,17,18,19,20 };
            List<Integer> list = binarySearch2(arr,1000);
            System.out.println("list:"+list);
        }
    
        private static int binarySearch(int[]arr,int findValue){
            return binarySearch(arr,0,arr.length-1,findValue);
        }
    
        /**
         * 二分查找算法
         * @param arr 被查找数组
         * @param left  左边的索引
         * @param right 右边的索引
         * @param findValue 需要查找的值
         * @return 如果找到就返回下标,如果没有找到,就返回 -1
         */
        private static int binarySearch(int[]arr,int left,int right,int findValue){
            if(left > right){
                return -1;
            }
            int mid = (left+right)/2;
            int midValue =arr[mid];
            if(findValue > midValue){
                return binarySearch(arr,mid+1,right,findValue);
            }else if(findValue < midValue){
                return binarySearch(arr,left,mid-1,findValue);
            }else {
                return mid;
            }
    
        }
    
        private static List<Integer> binarySearch2(int[]arr,int findValue){
            return binarySearch2(arr,0,arr.length-1,findValue);
        }
    
        //完成一个课后思考题:
        /*
         * 课后思考题: {1,8, 10, 89, 1000, 1000,1234} 当一个有序数组中,
         * 有多个相同的数值时,如何将所有的数值都查找到,比如这里的 1000
         *
         * 思路分析
         * 1. 在找到mid 索引值,不要马上返回
         * 2. 向mid 索引值的左边扫描,将所有满足 1000, 的元素的下标,加入到集合ArrayList
         * 3. 向mid 索引值的右边扫描,将所有满足 1000, 的元素的下标,加入到集合ArrayList
         * 4. 将Arraylist返回
         */
        private static List<Integer> binarySearch2(int[]arr,int left,int right,int findValue){
            if(left > right){
                return new ArrayList<Integer>();
            }
    
            int mid = (left+right)/2;
            int midValue = arr[mid];
            if(findValue > midValue){
                return binarySearch2(arr,mid+1,right,findValue);
            }else if (findValue < midValue){
                return binarySearch2(arr,left,mid-1,findValue);
            }else {
                //			 * 思路分析
                //			 * 1. 在找到mid 索引值,不要马上返回
                //			 * 2. 向mid 索引值的左边扫描,将所有满足 1000, 的元素的下标,加入到集合ArrayList
                //			 * 3. 向mid 索引值的右边扫描,将所有满足 1000, 的元素的下标,加入到集合ArrayList
                //			 * 4. 将Arraylist返回
                List<Integer>resList = new ArrayList<Integer>();
                int temp = mid-1;
                while (true){
                    //向mid 索引值的左边扫描,将所有满足 1000, 的元素的下标,加入到集合ArrayList
                    if(temp < 0 || arr[temp] !=findValue){
                        break;
                    }
                    //否则,就temp 放入到 resIndexlist
                    resList.add(temp);
                    //temp左移
                    temp-=1;
                }
    
                resList.add(mid);
                //向mid 索引值的右边扫描,将所有满足 1000, 的元素的下标,加入到集合ArrayList
                temp = mid+1;
                while (true){
                    if(temp > arr.length-1 || arr[temp] != findValue){
                        break;
                    }
                    //否则,就temp 放入到 resIndexlist
                    resList.add(temp);
                    temp+=1;
                }
                return resList;
    
            }
        }
    }
    
    结果是:
    D:jdk1_8jdkinjava.exe "-javaagent:D:soft_installideaIntelliJ IDEA 2020.1.3libidea_rt.jar=1201:D:soft_installideaIntelliJ IDEA 2020.1.3in" -Dfile.encoding=UTF-8 -classpath D:jdk1_8jdkjrelibcharsets.jar;D:jdk1_8jdkjrelibdeploy.jar;D:jdk1_8jdkjrelibextaccess-bridge.jar;D:jdk1_8jdkjrelibextcldrdata.jar;D:jdk1_8jdkjrelibextdnsns.jar;D:jdk1_8jdkjrelibextjaccess.jar;D:jdk1_8jdkjrelibextjfxrt.jar;D:jdk1_8jdkjrelibextlocaledata.jar;D:jdk1_8jdkjrelibext
    ashorn.jar;D:jdk1_8jdkjrelibextsunec.jar;D:jdk1_8jdkjrelibextsunjce_provider.jar;D:jdk1_8jdkjrelibextsunmscapi.jar;D:jdk1_8jdkjrelibextsunpkcs11.jar;D:jdk1_8jdkjrelibextzipfs.jar;D:jdk1_8jdkjrelibjavaws.jar;D:jdk1_8jdkjrelibjce.jar;D:jdk1_8jdkjrelibjfr.jar;D:jdk1_8jdkjrelibjfxswt.jar;D:jdk1_8jdkjrelibjsse.jar;D:jdk1_8jdkjrelibmanagement-agent.jar;D:jdk1_8jdkjrelibplugin.jar;D:jdk1_8jdkjrelib
    esources.jar;D:jdk1_8jdkjrelib
    t.jar;D:2_my_codejavadataStructureoutproductiondataStructure com.petrel.BinarySearch.BinarySearch
    index:5
    list:[4, 5]
    
    Process finished with exit code 0
    
  • 相关阅读:
    vs2022 搭建NET6 WebApi 接口项目《五》 接口访问限流配置
    vs2022 搭建NET6 WebApi 接口项目《二》 配置swagger
    vs2022 搭建NET6 WebApi 接口项目《一》 整体项目搭建
    vs2022 搭建NET6 WebApi 接口项目《六》 返回参数配置
    vs2022 搭建NET6 WebApi 接口项目《三》 配置跨域
    Docker 在mac上的使用
    构建煤矿物联网大数据平台思路(1)
    构建煤矿物联网大数据平台思路(2)实时数据库与时序数据库
    Redis 只能通过127.0.0.1访问,磁盘在使用中等问题...
    git 提交忽略bin obj等文件
  • 原文地址:https://www.cnblogs.com/myhy/p/13799983.html
Copyright © 2020-2023  润新知