-
二分查找也属于结果集缩小案例:
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
相关推荐
努力学习的小廉几秒前
我爱学算法之—— 回溯2301_76347246几秒前
C++中的享元模式高级应用我真的是大笨蛋1 分钟前
MVCC解析weixin_458923203 分钟前
分布式日志系统实现秃头续命码农人3 分钟前
谈谈对Spring、Spring MVC、SpringBoot、SpringCloud,Mybatis框架的理解ahauedu6 分钟前
SpringBoot 3.5.10引入springdoc-openapi-starter-webmvc-ui版本我是咸鱼不闲呀7 分钟前
力扣Hot100系列15(Java)——[二叉树]总结(有效的括号,最小栈,字符串解码,每日温度,柱状图中最大的矩形)C蔡博士12 分钟前
算法设计与分析:稳定配对(Stable Matching)问题拾光Ծ13 分钟前
【优选算法】双指针算法:专题二沉默-_-13 分钟前
MyBatis 学习笔记