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

  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);
      }
      }

相关推荐
heimeiyingwang25 分钟前
【架构实战】Kubernetes调度器深度剖析:从Pod调度到自定义调度器
java·架构·kubernetes
Jerry1 小时前
LeetCode 108. 将有序数组转换为二叉搜索树
算法
蓝斯4972 小时前
Diff算法的简单介绍
算法
过期的秋刀鱼!2 小时前
L2正则化,防止过拟合-历史补充
算法·过拟合·l2正则化
纳兰青华2 小时前
回文侦探:三种境界破解最长回文子串
java·算法·动态规划
hansang_IR2 小时前
【题解】LC:Z 算法(Z Algorithm)
c++·算法·字符串
范什么特西3 小时前
回答知识总结02
算法·哈希算法
橘子汽水1683 小时前
Leetcode 230,98:二叉搜索树中第K小的元素,验证二叉搜索树
算法·leetcode·职场和发展
Zane19943 小时前
CAS 与原子类:Java 如何实现无锁编程
java·后端
TunerT_TQ3 小时前
【智能体安全治理|专栏第9期】从“一堆规则”到“数字宪法”:智能体治理的下一个阶段
java·开发语言·安全·开源治理·大模型安全·ai基础设施·智能体安全