Java BinarySearch
Java BinarySearch
/**
* <html>
* <body>
* <P> Copyright 1994-2018 JasonInternational </p>
* <p> All rights reserved.</p>
* <p> Created on 2018年4月10日 上午9:46:32</p>
* <p> Created by Jason</p>
* </body>
* </html>
*/
package cn.ucaner.algorithm.search; /**
* In computer science, binary search, also known as half-interval search or logarithmic search, is a search algorithm that finds the position of a target value within a sorted array. Binary search
* compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is
* successful or the remaining half is empty.
* <p>
* Worst-case performance O(log n)<br>
* Best-case performance O(1)<br>
* Average performance O(log n)<br>
* Worst-case space complexity O(1)<br>
* <p>
* @see <a href="https://en.wikipedia.org/wiki/Binary_search_algorithm">Binary Search (Wikipedia)</a>
* <br>
* @author Justin Wetherell <phishman3579@gmail.com>
*/
public class BinarySearch { private static final int SWITCH_TO_BRUTE_FORCE = 200; private static int[] sorted = null; // Assuming the array is sorted
public static final int find(int value, int[] array, boolean optimize) {
BinarySearch.sorted = array;
try {
return recursiveFind(value, 0, BinarySearch.sorted.length - 1, optimize);
} finally {
BinarySearch.sorted = null;
}
} private static int recursiveFind(int value, int start, int end, boolean optimize) {
if (start == end) {
int lastValue = sorted[start]; // start==end
if (value == lastValue)
return start; // start==end
return Integer.MAX_VALUE;
} final int low = start;
final int high = end + 1; // zero indexed, so add one.
final int middle = low + ((high - low) / 2); final int middleValue = sorted[middle];
if (value == middleValue)
return middle;
if (value > middleValue) {
if (optimize && (end - middle) <= SWITCH_TO_BRUTE_FORCE)
return linearSearch(value, middle + 1, end);
return recursiveFind(value, middle + 1, end, optimize);
}
if (optimize && (end - middle) <= SWITCH_TO_BRUTE_FORCE)
return linearSearch(value, start, middle - 1);
return recursiveFind(value, start, middle - 1, optimize);
} private static final int linearSearch(int value, int start, int end) {
for (int i = start; i <= end; i++) {
int iValue = sorted[i];
if (value == iValue)
return i;
}
return Integer.MAX_VALUE;
}
}
Java BinarySearch的更多相关文章
- 配置算法(第4版)的Java编译环境
1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...
- 算法(第4版)-1.1.1 Java程序的基本结构
开始之前,引用书中的一段话: "学习算法的主要原因是它们能节约非常多的资源,甚至能够让我们完成一些本不可能完成的任务.在某些需要处理上百万个对象的应用程序,设计优良的算法甚至可以将程序运行的 ...
- Java(C#)基础差异-数组
1.填充数组 Java 数组填充替换方法Arrays.fill() 举例如下: import java.util.Arrays; public class FillDemo { public stat ...
- LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...
- 配置《算法 第四版》的Eclipse开发环境
1. 安装JAVA JAVA网址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 配置环境变量(我把JAVA安装在 ...
- 在Eclipse下配置算法(第四版)运行环境
第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...
- Eclipse直接运行算法第4版例子(重定向和读取指定路径文件)
Eclipse直接运行算法第4版例子(重定向和读取指定路径文件) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://b ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- JAVA Arrays.binarySearch
转自:http://blog.csdn.net/somebodydie/article/details/8229343 package com.jaky; import java.util.*; pu ...
随机推荐
- Flutter移动电商实战 --(46)详细页_自定义TabBar Widget
主要实现详情和评论的tab provide定义变量 自己做一个tab然后用provide去控制 定义两个变量来判断是左侧选中了还是右侧选中了.并定义一个方法来接受参数,修改是左侧还是右侧选中的状态值 ...
- 如何实现 Https拦截进行 非常规“抓包” 珍惜Any 看雪学院 今天 前段时间在自己做开发的时候发现一个很好用的工具,OKHttp的拦截器(何为拦截器?就是在每次发送网络请求的时候都会走的一个回调)大概效果如下:
如何实现 Https拦截进行 非常规“抓包” 珍惜Any 看雪学院 今天 前段时间在自己做开发的时候发现一个很好用的工具,OKHttp的拦截器(何为拦截器?就是在每次发送网络请求的时候都会走的一个回调 ...
- UVA:1600 巡逻机器人
机器人要从一个m*n(m和n的范围都在1到20的闭区间内)的网格的左上角(1,1)走到右下角(m,n).网格中的一些格子是空地,用0表示,其它格子是障碍,用1表示.机器人每次可以往四个方向走一格,但不 ...
- 机器学习 - 算法 - SVM 支持向量机
SVM 原理引入 支持向量机( SVM,Support Vector Machine ) 背景 2012年前较为火热, 但是在12年后被神经网络逼宫, 由于应用场景以及应用算法的不同, SVM还是需要 ...
- vs2015配置link.exe环境变量
https://www.cnblogs.com/johnwii/p/4966086.html
- python迭代器、生成器、装饰器之迭代器
迭代是Python最强大的功能之一,是访问集合元素的一种方式. 一般分为可迭代对象,迭代器,可迭代对象不一定是迭代器,但迭代器一定是可迭代对象 1.可以直接作用于for循环的数据类型 第一类:集合数据 ...
- Jrebel激活方法
参考 https://www.yanjiayu.cn/posts/3eecb801.html https://gitee.com/gsls200808/JrebelLicenseServerforJa ...
- React16+配置打包目录
在学习react的时候,肯定最终都要用脚手架,对比了手写配置webpack(比较费劲).generator-react-webpack(打不开官方文档……),最终还是选择了react官方的create ...
- 在C/C++中常用的符号
C++中&和*的用法一直是非常让人头疼的难点,课本博客上讲这些知识点一般都是分开讲其用法的,没有详细的总结,导致我在这方面的知识结构格外混乱,在网上找到了一篇英文文章简单总结了这两个符号的一些 ...
- linux 环境下jmeter+ant+jenkins
一.linux下的jenkins的安装: 下载链接:https://pan.baidu.com/s/1qZItZOC 密码:58da Jenkins 下载网址: http://jenkins-ci.o ...