-
二分查找也属于结果集缩小案例:
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
相关推荐
咖啡八杯1 小时前
GoF设计模式——享元模式小小工匠1 小时前
Redis - 事务机制:能实现 ACID 属性吗十五喵源码网1 小时前
基于springboot2+vue2的租房管理系统摇滚侠1 小时前
IDEA 创建 Java 项目 手动整合 SSM 框架源分享1 小时前
Java线程同步的多种实现方法(非常详细)Flittly2 小时前
【AgentScope Java新手村系列】(10)实战-多Agent天气助手李少兄2 小时前
从原理到实战:Spring IoC/DI 核心知识体系与高频面试题全解玖玥拾2 小时前
C/C++ 数据结构(七)栈、容器适配器何以解忧,唯有..2 小时前
Go语言循环语句详解:for、range与循环控制飞天狗1112 小时前
零基础JavaWeb入门——第五课第二小节:九大内置对象 · 第2个:response(响应对象)