-
二分查找也属于结果集缩小案例:
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
相关推荐
Wang's Blog6 小时前
Java 项目实战: 交友项目 - FastDFS:入门案例apcipot_rain6 小时前
C++转Java日记(03)皮皮林5516 小时前
Java实现微信扫码登录布莱克6056 小时前
C++ 中 map 的 insert 与 emplace:key 已存在时会覆盖吗?Yyyyyy~9 小时前
【Java】Day4Web基础知识D e re k ZH9 小时前
C++ 系统学习手记:从语法细节到工程实践灿飞屋10 小时前
Kotlin与Java虚拟机三:控制流石小石Orz10 小时前
AI 正在蚕食我们的编码能力NE_STOP11 小时前
Spring AI Alibaba - MCP入门