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的更多相关文章

  1. 配置算法(第4版)的Java编译环境

    1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...

  2. 算法(第4版)-1.1.1 Java程序的基本结构

    开始之前,引用书中的一段话: "学习算法的主要原因是它们能节约非常多的资源,甚至能够让我们完成一些本不可能完成的任务.在某些需要处理上百万个对象的应用程序,设计优良的算法甚至可以将程序运行的 ...

  3. Java(C#)基础差异-数组

    1.填充数组 Java 数组填充替换方法Arrays.fill() 举例如下: import java.util.Arrays; public class FillDemo { public stat ...

  4. LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)

    题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...

  5. 配置《算法 第四版》的Eclipse开发环境

    1. 安装JAVA JAVA网址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 配置环境变量(我把JAVA安装在 ...

  6. 在Eclipse下配置算法(第四版)运行环境

    第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...

  7. Eclipse直接运行算法第4版例子(重定向和读取指定路径文件)

    Eclipse直接运行算法第4版例子(重定向和读取指定路径文件)   版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://b ...

  8. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  9. JAVA Arrays.binarySearch

    转自:http://blog.csdn.net/somebodydie/article/details/8229343 package com.jaky; import java.util.*; pu ...

随机推荐

  1. 图片上传利用request.getInputStream()获取文件流时遇到的问题

    图片上传功能是我们web里面经常用到的,获得的方式也有很多种,这里我用的是request.getInputStream()获取文件流的方式.想要获取文件流有两种方式,附上代码 int length = ...

  2. swoole的websockte例子

    服务器的环境,建议用bt.cn的一键环境 服务端: <?php /** * Created by PhpStorm. * User: Administrator * Date: 2019\5\2 ...

  3. spring cloud+.net core搭建微服务架构

    http://www.cnblogs.com/longxianghui/p/7800316.html

  4. Java多线程中thread.getname()和thread.currentThread().getName();的去别

    首先要明白 this.XXX 的使用场景 使用Thread.currentThread().getName()和使用this.getName()和对象实例.getName(),都可以得到线程的名称,但 ...

  5. springboot下jar包方式运行Caused by: java.lang.ExceptionInInitializerError: null

    idea调试过程中不会出现此问题,异常如下 org.springframework.beans.factory.BeanCreationException: Error creating bean w ...

  6. Go 自定义包引入报错

    配置文件 GO111MODULE=on 设置为on时,go命令行会使用modules,而一点也不会去GOPATH目录下查找.但自定义包在 $GOPATH/github.com/winyh/strrev ...

  7. Permission denied: user=dr.who, access=READ_EXECUTE, inode="/tmp":student:supergroup:drwx------权限问题

    在查看browse directory时,点击tmp,无法进入,报错:“Permission denied: user=dr.who, access=READ_EXECUTE, inode=" ...

  8. 【Leetcode_easy】674. Longest Continuous Increasing Subsequence

    problem 674. Longest Continuous Increasing Subsequence solution class Solution { public: int findLen ...

  9. web前端常用meta整理

    标签提供关于HTML文档的元数据.元数据不会显示在页面上,但是对于机器是可读的.它可用于浏览器(如何显示内容或重新加载页面),搜索引擎(关键词),或其他 web 服务. 页面关键词 <meta ...

  10. Python 解LeetCode:744. Find Smallest Letter Greater Than Target

    思路:二分法,时间复杂度o(logn) class Solution(object): def nextGreatestLetter(self, letters, target): "&qu ...