您现在的位置是:首页 > 后台技术 > 数据结构与算法数据结构与算法

非递归二分查找算法(图文)

第十三双眼睛2023-10-22【数据结构与算法】人已围观

简介非递归二分查找算法

非递归二分查找算法
package com.xingchen.day016;
public class BinSearchNoRecur {
    public static void main(String[] args) {
        int[] arr = {1,3,8,10,11,67,100};
        int result = binSearch(arr, 110);
        System.out.println(result);
    }
    public static int binSearch(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;
    }
}

Tags:

很赞哦! ()

文章评论

    共有条评论来说两句吧...

    用户名:

    验证码:

本站推荐

站点信息

  • 网站名称:JavaStudy
  • 建站时间:2019-1-14
  • 网站程序:帝国CMS7.5
  • 文章统计242篇文章
  • 标签管理标签云
  • 统计数据百度统计
  • 微信公众号:扫描二维码,关注我们