js binary search algorithm

js 二分查找算法

二分查找, 前置条件

  1. 存储在数组中
  2. 有序排列

理想条件: 数组是递增排列,数组中的元素互不相同;

重排 & 去重

顺序: 递增排列/递减排列;

重复: 数组中存在相同的元素;


"use strict"; /**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-05-20
* @modified
*
* @description 二分查找 binary-search
* @augments
* @example
* @link
*
*/ const log = console.log; function binarySearch(data, key, preIndex = 0, debug = false){
let len = data.length;
// 向下取整
let half = Math.floor(len/2);
// 差值
let diffValue = key - data[half];
if(debug) {
// preIndex 保留上次的索引值 indexOf
log(`\npreIndex`, preIndex)
log(`len`, len)
log(`half`, half)
log(`key`, key)
log(`data[half]`, data[half])
// number - undefined == NaN
log(`diffValue`, diffValue)
}
if (diffValue > 0) {
preIndex = preIndex + half + 1;
let right = data.slice(half + 1);
return binarySearch(right, key, preIndex);
} else if (diffValue < 0) {
let left = data.slice(0, half);
return binarySearch(left, key, preIndex);
}else if (diffValue === 0) {
return preIndex + half;
} else {
return -1;
}
} let v4 = binarySearch([1,3,5,7,9], 9);
log(`v4`, v4)
// 4 let v11 = binarySearch([1,3,5,7,9], 11);
log(`v11`, v11)
// -1 // binarySearch(5, [1,2,3,4,5,6,7,8]);
// // 4
// binarySearch(5, [1,3,5,7,9]);
// // 2
// binarySearch(1, [1,3,5,7,9]);
// // 0
// binarySearch(7, [1,3,5,7,9]);
// // 3
// binarySearch(9, [1,3,5,7,9]);
// // -1

BST


function binarySearch(target,arr,start,end) {
if( start > end){
return -1
}
var start = start || 0;
var end = end || arr.length-1;
var mid = parseInt(start + (end-start)/2);
if(target==arr[mid]){
return mid;
}else if(target>arr[mid]){
return binarySearch(target, arr, mid+1, end);
}else{
return binarySearch(target, arr, start, mid-1);
}
return -1;
} binarySearch(9,[1,2,3,4,5,7,8])
// -1

https://www.jianshu.com/p/eef65b21ace0

Big O



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


js binary search algorithm的更多相关文章

  1. [Algorithms] Binary Search Algorithm using TypeScript

    (binary search trees) which form the basis of modern databases and immutable data structures. Binary ...

  2. 【437】Binary search algorithm,二分搜索算法

    Complexity: O(log(n)) Ref: Binary search algorithm or 二分搜索算法 Ref: C 版本 while 循环 C Language scripts b ...

  3. [Math] Beating the binary search algorithm – interpolation search, galloping search

    From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下 ...

  4. [Algorithm] Beating the Binary Search algorithm – Interpolation Search, Galloping Search

    From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下 ...

  5. Binary Search Algorithm

    二分查找代码: //============================================================================ // Name : Bin ...

  6. [UCSD白板题] Binary Search

    Problem Introduction In this problem, you will implemented the binary search algorithm that allows s ...

  7. Foundation: Binary Search

    /* Binary search. * * Implementation history: * 2013-10-5, Mars Fu, first version. */ /* [Binary Sea ...

  8. What's binary search?

    Binary Search: Search a sorted array by repeatedly  dividing the search interval in half. Begin with ...

  9. Binary Search - Jump on the Stones

    Binary Search algorithm. Wikipedia definition: In computer science, binary search, also known as hal ...

随机推荐

  1. c++ 三五法则 自己理解

    简介 三五法则规定了什么时候需要  1 拷贝构造函数   2 拷贝赋值函数  3 析构函数 1. 需要析构函数的类也需要拷贝构造函数和拷贝赋值函数. 通常,若一个类需要析构函数,则代表其合成的析构函数 ...

  2. Java——时间和日期处理

    Date Date date = new Date(); 获取时间 Date d = new Date(); // Date d2=new Date(System.currentTimeMillis( ...

  3. 编写高性能Java代码的最佳实践

    博客地址: http://blog.csdn.net/dev_csdn/article/details/79033972

  4. MySQL、SqlServer、Oracle 三种数据库的优缺点

    MySQL.SqlServer.Oracle 三种数据库的优缺点 一.MySQL 优点: 缺点: 二.SqlServer 优点: 缺点: 三.Oracle 优点: 缺点: 一.MySQL 优点: 体积 ...

  5. Django(图书管理系统)

    图书管理系统 注意事项 1.models 要创建好,规划好自己的表,以及各种表关系 2.url正则要写好 3.settings的配置 4.利用bootstarp 进行布局更漂亮哦 5.注意orm  各 ...

  6. linux 用户、用户组及相关命令(useradd 、passwd、userdel 、groupadd 、groupdel、usermod 、gpasswd 、 id、su)

    linux是一个多用户系统,用于权限管理(权限最小化); 相关命令: 7 8 9 10 11 12 13 14 15 useradd passwd userdel groupadd groupdel ...

  7. 删除无用docker镜像

    docker images | grep none | grep -v grep | awk '{print $3}' | xargs docker rmi -f

  8. 给jekyll博客添加搜索功能

    使用SWIFTYPE为jekyll博客添加搜索引擎 步骤 1.首先去swiftype注册一个账号 2.接着添加自己想要配置的网站地址并为新设定的引擎添加一个名字(非会员只能设置一个引擎). 3.收到验 ...

  9. 设计模式(九)——装饰者模式(io源码分析)

    1 星巴克咖啡订单项目(咖啡馆): 1) 咖啡种类/单品咖啡:Espresso(意大利浓咖啡).ShortBlack.LongBlack(美式咖啡).Decaf(无因咖啡) 2) 调料:Milk.So ...

  10. Java-Graphics类的绘图方法实现

    Java-Graphics(画图类) 就比如画一个矩形,你给出矩形左上角坐标,再给出矩形长度和宽度就可以在JFrame上画出来一个矩形 除了矩形之外,还可以画椭圆.圆.圆弧.线段.多边形.图像等 下面 ...