-
二分查找也属于结果集缩小案例:
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
相关推荐
TTGGGFF3 小时前
控制系统建模仿真(四):线性控制系统的数学模型正在努力Coding3 小时前
SpringAI - 工具调用晚风吹长发3 小时前
初步了解Linux中的命名管道及简单应用和简单日志我尽力学4 小时前
面试 总结爬台阶的蚂蚁4 小时前
Spring AI Alibaba基础概念Σίσυφος19004 小时前
Halcon中霍夫直线案例计算机学姐4 小时前
基于SpringBoot的演唱会抢票系统huohuopro4 小时前
Mybatis的七种传参方式Lee_SmallNorth4 小时前
变态需求之【角色不同访问数据库的用户不同】夏乌_Wx4 小时前
练题100天——DAY42:移除链表元素 ★★☆☆☆