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. OpenTK学习笔记(3)-你好!窗体!

    参考:http://dreamstatecoding.blogspot.com/2017/01/opengl-4-with-opentk-in-c-part-1.html http://www.cnb ...

  2. ISO/IEC 9899:2011 条款6.2.7——兼容类型与组合类型

    6.2.7 兼容类型与组合类型 1.两个类型具有兼容类型,如果它们的类型是相同的.用于判定两个类型是否兼容的其它规则在6.7.2关于类型说明符中,6.7.3关于类型说明符中,6.7.6关于声明符中描述 ...

  3. 19 Flutter 自定义AppBar 定义顶部Tab切换 底部Tab结合顶部Tab实现类似头条页面布局(27分36秒)

    Flutter AppBar自定义顶部导航按钮图标.颜色以及TabBar定义顶部Tab切换. leading:在标题前面显示的一个控件,在首页通常显示应用的logo:在其他界面通常显示为付汇按钮. t ...

  4. React之简介

    官网链接React 用于构建用户界面的 JavaScript 库 特色 声明式: React 使创建交互式 UI 变得轻而易举.为你应用的每一个状态设计简洁的视图,当数据改变时 React 能有效地更 ...

  5. std::function以及std::bind

    转自:https://blog.csdn.net/shuilan0066/article/details/82788954 示例1 : 普通函数 void gFunc() { cout << ...

  6. 使用wsimport生成webservice客户端代码

    服务端 package com.xc.webservice; import javax.jws.WebService; import javax.xml.ws.Endpoint; @WebServic ...

  7. win10系统安装踩坑之路

    1.一定要下载win10原版镜像.如果用迅雷下载一定要校验文件hash值的完整性,可以用fhash.exe校验,如果哈希值不一致,一定要重新下载镜像. 2.用软媒U盘启动制作启动U盘 3.重启后按F1 ...

  8. Spring Boot使用JDBC方式连接MySQL

    首先去spring官网下载一个名为test的Spring Boot项目模板:https://start.spring.io/ 然后在mysql中的testdb数据库中新建一张名为test_user的表 ...

  9. 【Leetcode_easy】637. Average of Levels in Binary Tree

    problem 637. Average of Levels in Binary Tree 参考 1. Leetcode_easy_637. Average of Levels in Binary T ...

  10. 【Leetcode_easy】628. Maximum Product of Three Numbers

    problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...