zoukankan      html  css  js  c++  java
  • 二分查找非递归

    package com.liuhuan.study.algorithms.search;
    
    /**
     * @author LiuHuan
     * @date 2020-07-09 14:35
     * @desc 二分查找非递归
     */
    public class BinarySearchNoRecur {
    
        public static void main(String[] args) {
            //测试
            int[] arr = {1, 3, 8, 10, 11, 67, 100};
            int index = binarySearch(arr, 100);
            System.out.println("index=" + index);
        }
    
        /**
         * 二分查找的非递归实现
         *
         * @param arr    待查找的数组, arr是升序排序
         * @param target 需要查找的数
         * @return 返回对应下标,-1表示没有找到
         */
        public static int binarySearch(int[] arr, int target) {
    
            int left = 0;
            int right = arr.length - 1;
            //说明继续查找
            while (left <= right) {
                int mid = (left + right) / 2;
                if (arr[mid] == target) {
                    return mid;
                } else if (arr[mid] > target) {
                    //需要向左边查找
                    right = mid - 1;
                } else {
                    //需要向右边查找
                    left = mid + 1;
                }
            }
            return -1;
        }
    
    }
    
    
  • 相关阅读:
    echarts 饼图
    vue echarts
    :style :class
    弹框弹出时禁止页面滚动
    2019-2-10 日记
    2019-1-27 日记
    2019-1-26 日记
    2019-1-3 日记
    2019-1-10 日记
    2019-1-2 日记
  • 原文地址:https://www.cnblogs.com/ding-dang/p/13395161.html
Copyright © 2011-2022 走看看