-
二分查找也属于结果集缩小案例:
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
相关推荐
2501_903238656 分钟前
Java 9模块开发:Eclipse实战指南励志成为美貌才华为一体的女子14 分钟前
python算法和数据结构刷题[4]:查找算法和排序算法test猿23 分钟前
hive为什么建表,表存储什么tt55555555555541 分钟前
每日一题-判断是不是完全二叉树程序猿零零漆1 小时前
SpringCloud系列教程:微服务的未来(二十)Seata快速入门、部署TC服务、微服务集成Seata嘻嘻哈哈的zl2 小时前
初级数据结构:栈和队列小王努力学编程2 小时前
【C++篇】哈希表君义_noip2 小时前
信息学奥赛一本通 1607:【 例 2】任务安排 2 | 洛谷 P10979 任务安排 2我的K84092 小时前
Spring Boot基本项目结构因兹菜2 小时前
[LeetCode]day4 977.有序数组的平方