1 Star 1 Fork 1

xcc / structures-and-algorithm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Demo01_BinarySearchWithOutRecursion.java 712 Bytes
一键复制 编辑 原始数据 按行查看 历史
xcc 提交于 2020-12-31 16:05 . 修改目录路径
package com.xcc.dataStructures.demo14_algapplication;
/**
* 非递归的二分查找算法
*/
public class Demo01_BinarySearchWithOutRecursion {
public static void main(String[] args) {
int[] arr = {1, 3, 5, 7, 10, 15};
System.out.println(search(arr, 8));
}
public static int search(int[] arr, int val) {
int left = 0;
int right = arr.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] == val) {
return mid;
} else if (arr[mid] > val) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
}
}
1
https://gitee.com/xiaocheng0902/structures-and-algorithm.git
git@gitee.com:xiaocheng0902/structures-and-algorithm.git
xiaocheng0902
structures-and-algorithm
structures-and-algorithm
master

搜索帮助