• 算法——线性查找


    线性查找也叫顺序查找,这是最基本的一种查找方法,从给定的值中进行搜索,从一端开始逐一检查每个元素,直到找到所需元素的过程。

      如果元素个数为 N,那么线性查找的平均次数为: N/2

      下面通过一个例子,演示线性查找:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
     
    public class SequentialSearch {
        //定义一个数组,存放所有数据
        static int[] array = null;
        //定义一个 set 集合,用来排除数组中重复的数
        Set<Integer> setNum = new HashSet<>();
        //定义一个整型数,表示查找次数
        int times = 0;
        /**
         * 根据传入的 num 随机产生 num 个不重复的数
         * @param num
         * @return
         */
        public int[] createArray(int num){
            array = new int[num];
            while(setNum.size()!=num){
                int temp = (int)(Math.random()*10*num);
                setNum.add(temp);
            }
             
            int j = 0;
            Iterator<Integer> it = setNum.iterator();
            while(it.hasNext()){
                array[j] = it.next();
                j++;
            }
             
            return array;
        }
         
        /***
         * 顺序查找
         * @return
         */
        public int seqSearch(int num){
            array = createArray(10);//产生包含 10 个随机数的 数组
            printArray(array);  //打印产生的随机数组
            for(int i = 0 ; i< array.length ; i++){
                if(array[i] == num){
                    times = i+1;    //如果找到了,返回找到的次数
                }else{
                    times = -1//如果没找到,返回 -1
                }
            }
            return times;
        }
         
        /***
         * 打印数组
         * @param array
         */
        public void printArray(int [] array){
            if(array.length == 0){
                System.out.println("[]");
            }else{
                StringBuilder sb = new StringBuilder();
                sb.append('[');
                for(int i = 0 ; i < array.length ; i++){
                    if(i == array.length-1){
                        sb.append(array[i]+"]");
                    }else{
                        sb.append(array[i]+",");
                    }
                }
                System.out.println(sb);
            }
        }
         
         
        public static void main(String[] args) {
            SequentialSearch ss = new SequentialSearch();
            System.out.println(ss.seqSearch(29)); //随机查找一个数,比如 29
        }
     
    }

    作者:KeerDi —— 北方的后生

    出处:http://www.cnblogs.com/keerdi/

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    分布式事务解决方案1--使用Atomikos分布式事务(事务强一致方案)
    SringBoot集成Sharding-Jdbc 实践
    Sharding-Jdbc简介
    Mycat+haproxy中使用keepalived保障haproxy的高可用
    Angular CLI
    背压(Backpressure)机制
    Function.identity()
    解决Error: ENOENT: no such file or directory, scandir 安装node-sass报错
    Reactor flatMap
    Reactor map
  • 原文地址:https://www.cnblogs.com/123hll/p/6903190.html
Copyright © 2020-2023  润新知