数据结构 递归解决二分查找 数据结构(八)

  1. 二分查找也属于结果集缩小案例:

    package com.nami.algorithm.study.day06;

    /**

    • beyond u self and trust u self.

    • @Author: lbc

    • @Date: 2023-09-05 10:09

    • @email: 594599620@qq.com

    • @Description: keep coding
      */
      public class BinarySearch {

      private static int search(int[] target, int search, int i, int j) {
      if (i > j) {
      return -1;
      }
      int temp = (i + j) >>> 1;
      if (target[temp] < search) {
      return search(target, search, temp + 1, j);
      } else if (target[temp] > search) {
      return search(target, search, i, temp - 1);
      } else {
      return temp;
      }

      }

      public static int search0(int[] target, int search) {
      return search(target, search, 0 , target.length);
      }

      public static void main(String[] args) {
      int[] target = new int[]{1, 2, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 19, 23, 24, 25, 26, 27, 28, 57, 234};
      int result = search0(target, 234);
      System.out.println(result);
      }
      }

相关推荐
zl_vslam1 分钟前
SLAM中的非线性优-3D图优化之绝对位姿SE3约束SO3/t形式(十八)
人工智能·算法·计算机视觉·3d
啊阿狸不会拉杆1 分钟前
《计算机操作系统》 - 第九章 操作系统接口
人工智能·算法·计算机组成原理·os·计算机操作系统
dazzle2 分钟前
Python数据结构(六):双端队列详解
开发语言·数据结构·python
一起养小猫3 分钟前
Flutter for OpenHarmony 实战:碰撞检测算法与游戏结束处理
算法·flutter·游戏
刃神太酷啦3 分钟前
Linux 基础 IO 收官:库的构建与使用、进程地址空间及核心知识点全解----《Hello Linux!》(11)
java·linux·c语言·数据库·c++·算法·php
板面华仔4 分钟前
机器学习入门(一)——KNN算法
人工智能·算法·机器学习
猿小羽7 分钟前
Spring AI + MCP 实战:构建标准化、可扩展的 AI Agent 架构体系
java·spring boot·llm·架构设计·ai agent·spring ai·mcp
独自破碎E10 分钟前
Spring Boot的多环境配置
java·spring boot·后端
宵时待雨11 分钟前
数据结构(初阶)笔记归纳8:栈和队列
数据结构·笔记
拽着尾巴的鱼儿12 分钟前
Idea-Spring-boot 项目启动无法识别lombok
java·intellij-idea