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 ...
随机推荐
- sql server 使用SqlBulkCopy批量插入数据库
sql server sqlbulkcopy 批量数据插入数据库使用的是System.Data.SqlClient中的 SqlBulkCopy批量数据插入数据库 sql server 使用SqlBul ...
- Tosca TestCases: Update all,Checkin all,Checkout,Checkout Tree
#记录一下TestCases Module 红框里面这几个button的功能 #Update all 从数据库里把别人最新checkin的东西给拽出来查看,也就是拿最新版本 #Checkin all ...
- BitmapFactory: inSampleSize 的一些思考
一. BitmapFactory.Options 中inSampleSize的取值问题 关于inSampleSize的取值问题Google已经给出了一个推荐的算法:(https://developer ...
- SQL-W3School-函数:SQL FIRST() 函数
ylbtech-SQL-W3School-函数:SQL FIRST() 函数 1.返回顶部 1. FIRST() 函数 FIRST() 函数返回指定的字段中第一个记录的值. 提示:可使用 ORDER ...
- MERN——MongoDB && React && Node && Express
原文链接:Let’s build a full stack MongoDB, React, Node and Express (MERN) app github源码地址:jelorivera08/re ...
- toast提示信息获取和Monkey笔记
获取toast toast提示信息出现场景:用户输入用户名和密码后,提示的'登录成功', 用之前的定位方法获取不了,需要Uiautomator2来获取 安装node.js (使用 npm 或 node ...
- APPSCAN使用外部浏览器的方法
在使用appscan扫描时,自带浏览器可能存在兼容性问题(比如HTML5),故需要用到其他浏览器.在做139邮箱HTML5项目,需要使用chrome浏览器进行扫描.因此分享下如何使用外部浏览器,将之前 ...
- SpringCloud学习成长之 九 服务链路跟踪
这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 一.简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案, ...
- Python - Django - 显示作者列表
在 views.py 中添加展示作者列表的函数 from django.shortcuts import render, redirect, HttpResponse from app01 impor ...
- WordPress的摘要显示方式
WordPress的后台有摘要显示的栏目,如果没有显示,需要设置一下,如图 在列表页现实的内容的时候,一般是有摘要的显示摘要,没有摘要的截取文章的前一部分文字.代码如下 <?php if(has ...