zoukankan      html  css  js  c++  java
  • 查找

    package com.kaibing.sortandfind;
    
    import org.junit.jupiter.api.Test;
    
    public class Find {
    
    
        public int[] arr = {1, 2, 3, 4, 5};
    
    
        @Test
        public void binaryFind() {
            System.out.println(binaryFindBody(0, arr.length - 1, arr, 58));
        }
    
        /**
         * 二分查找:基本思想是把数组分成两个分别查找
         * 时间:平均=O(log2n) | 最坏=O(log2n)
         * 空间:O(log2n)
         * 稳定性:稳定
         */
        public int binaryFindBody(int head, int tail, int[] arr, int find) {
    
            if (head > tail) {
                return -1;
            }
    
            int target = (head + tail) / 2;
    
            if (arr[target] == find) {
                return target;
            } else if (arr[target] > find && target > 0) {
                return binaryFindBody(head, target - 1, arr, find);
            } else {
                return binaryFindBody(target + 1, tail, arr, find);
            }
        }
    
    
    }
  • 相关阅读:
    Hadoop
    Mapreduce
    ssh原理
    HDFS
    Centos
    创建jira插件
    新型的领导者是一名推动者,而不是一名发号施令者
    上善若水,虚怀若谷
    GoAhead 嵌入式web
    Eclipse基金会
  • 原文地址:https://www.cnblogs.com/kaibing/p/9243151.html
Copyright © 2011-2022 走看看