-
二分查找也属于结果集缩小案例:
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
相关推荐
ShineWinsu5 小时前
对于 C++:C++14中从变量模板、泛型 Lambda 到并发与字面量的解析CHANCE V5 小时前
集合排序和流排序土司大王5 小时前
LeetCode hot100——合并两个有序链表码行山野赴时序归途5 小时前
算法效率的度量(上):时间复杂度中的对数思维16月6日-晴5 小时前
Java面向对象进阶—多态算AI6 小时前
基于LLM的无人机仿真测试:新方法竞赛夺佳绩qq_185198696 小时前
SpringBoot-五-AOT王大大的刀6 小时前
Spring AI 重试引起的 LLM 重复调用风流 少年6 小时前
hutoolH_oRIZoN_7 小时前
Linux入门DAY27(文件IO(系统调用)详解|open/read/write/lseek)