js binary search algorithm
js binary search algorithm
js 二分查找算法
二分查找, 前置条件
- 存储在数组中
- 有序排列
理想条件: 数组是递增排列,数组中的元素互不相同;
重排 & 去重
顺序: 递增排列/递减排列;
重复: 数组中存在相同的元素;
"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的更多相关文章
- [Algorithms] Binary Search Algorithm using TypeScript
(binary search trees) which form the basis of modern databases and immutable data structures. Binary ...
- 【437】Binary search algorithm,二分搜索算法
Complexity: O(log(n)) Ref: Binary search algorithm or 二分搜索算法 Ref: C 版本 while 循环 C Language scripts b ...
- [Math] Beating the binary search algorithm – interpolation search, galloping search
From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下 ...
- [Algorithm] Beating the Binary Search algorithm – Interpolation Search, Galloping Search
From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下 ...
- Binary Search Algorithm
二分查找代码: //============================================================================ // Name : Bin ...
- [UCSD白板题] Binary Search
Problem Introduction In this problem, you will implemented the binary search algorithm that allows s ...
- Foundation: Binary Search
/* Binary search. * * Implementation history: * 2013-10-5, Mars Fu, first version. */ /* [Binary Sea ...
- What's binary search?
Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with ...
- Binary Search - Jump on the Stones
Binary Search algorithm. Wikipedia definition: In computer science, binary search, also known as hal ...
随机推荐
- shiro的授权与认证
shiro的授权与认证 package com.cy.pj.common.aspect;import java.lang.reflect.Method;import java.util.Arrays; ...
- nginx http模块开发入门
导语 本文对nginx http模块开发需要掌握的一些关键点进行了提炼,同时以开发一个简单的日志模块进行讲解,让nginx的初学者也能看完之后做到心里有谱.本文只是一个用作入门的概述. 目录 背景 主 ...
- 浅谈自动化构建之gulp
一.gulp的基本使用 gulp是目前最流行的前端自动化构建系统,核心特点高效易用.(这块不过多的废话了,直接上干货了,有兴趣的话,可以查下gulp简介) 步骤如下: yarn init -y yar ...
- Trie(字典树)
没时间整理了,老吕又讲课了@ @ 概念 Trie即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种,典型应用是统计和排序大量的字符串(不限于字符串) Trie字典树主要用于存储字符串, ...
- 洛谷 p3391
题目背景 这是一道经典的Splay模板题--文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- 如何学习Java?从标识符开始
标识符 1.定义 Java对各种变量.方法和类等要素命名时使用的字符序列称为标识符(包含但不限于:类名.变量名.方法名.接口名.包名--) 2.命名规则 1.由26个英文字母大小写,0-9,_或$组成 ...
- Educational Codeforces Round 85 (Rated for Div. 2)
\(Educational\ Codeforces\ Round\ 85\ (Rated\ for\ Div.2)\) \(A. Level Statistics\) 每天都可能会有人玩游戏,同时一部 ...
- Codeforces Round #345 (Div. 1) C. Table Compression (并查集)
Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorith ...
- Codeforces Round #660 (Div. 2) Captain Flint and Treasure 拓扑排序(按照出度、入读两边拓扑排序)
题目链接:Captain Flint and Treasure 题意: 一种操作为 选一个下标 使得ans+=a[i] 且 把a[b[i]]+a[i] 要求每个下标都进行一种这样的操作,问怎么样的 ...
- poj1821——Fence
题意: 一个栅栏一共有n(从1--n)个木板,我们找k个工人去粉刷它,li表示每个人有限制粉刷木板数量,pi表示粉刷一个木板得到的钱,si表示他开始在那个木板前面 如果一个工人要粉刷,那么他必须粉刷s ...