• 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;
        }
    }
    

      

  • 相关阅读:
    day 66 ORM django 简介
    day 65 HTTP协议 Web框架的原理 服务器程序和应用程序
    jQuery的事件绑定和解绑 事件委托 轮播实现 jQuery的ajax jQuery补充
    background 超链接导航栏案例 定位
    继承性和层叠性 权重 盒模型 padding(内边距) border(边框) margin 标准文档流 块级元素和行内元素
    属性选择器 伪类选择器 伪元素选择器 浮动
    css的导入方式 基础选择器 高级选择器
    03-body标签中相关标签
    Java使用内存映射实现大文件的上传
    正则表达式
  • 原文地址:https://www.cnblogs.com/jasonandy/p/9243219.html
Copyright © 2020-2023  润新知