-
二分查找也属于结果集缩小案例:
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);
}
}
-
数据结构 递归解决二分查找 数据结构(八)
qq_339191142023-09-11 21:22
相关推荐
likerhood11 分钟前
java中的return this、链式编程和Builder模式spring29979212 分钟前
Spring Boot 实战篇(四):实现用户登录与注册功能王老师青少年编程13 分钟前
csp信奥赛C++高频考点专项训练之贪心算法 --【排序贪心】:拼数炽烈小老头31 分钟前
【 每天学习一点算法 2026/04/21】螺旋矩阵未来转换1 小时前
基于A2A协议的生产应用实践指南(Java)后端漫漫1 小时前
Redis 配置文件与服务功能Dwzun1 小时前
基于Java+SpringBoot+Vue的校园二手物品置换系统设计与实现【附源码+文档+部署视频+讲解】polaris06301 小时前
Spring Boot 项目开发流程全解析谭欣辰1 小时前
AC自动机:多模式匹配的高效利器zuowei28891 小时前
spring实例化对象的几种方式(使用XML配置文件)