Binary search is an algorithm that accepts a sorted list and returns a search element from the list. It provides a dramatic performance boost over searching linearly through a list for an element. Let’s play around number of iterations required for each search method to complete and refactor our linear list search into a binary search function.

let items = [10,5,6,7,1,3,2,4];
items = items.sort((a,b) => {return a-b}) function binarySearch (list, item = null) {
let low = 0;
let high = list.length;
let counter = 0; while (low <= high) {
counter++;
console.log(counter)
let med = Math.floor((low + high) / 2)
let guess = list[med];
if (guess === item) return true;
if (guess > item) high = med - 1;
else low = med + 1
} return null
} console.log(binarySearch(items,3));

[Algorithms] Refactor a Linear Search into a Binary Search with JavaScript的更多相关文章

  1. 04-树7. Search in a Binary Search Tree (25)

    04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...

  2. pat04-树7. Search in a Binary Search Tree (25)

    04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...

  3. 【Leetcode_easy】700. Search in a Binary Search Tree

    problem 700. Search in a Binary Search Tree 参考1. Leetcode_easy_700. Search in a Binary Search Tree; ...

  4. 39. Recover Binary Search Tree && Validate Binary Search Tree

    Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...

  5. 41. Unique Binary Search Trees && Unique Binary Search Trees II

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  6. Search Range in Binary Search Tree

    Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all t ...

  7. Lintcode: Search Range in Binary Search Tree

    Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all t ...

  8. Unique Binary Search Trees,Unique Binary Search Trees II

    Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given  ...

  9. Leetcode:Unique Binary Search Trees & Unique Binary Search Trees II

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

随机推荐

  1. Sublime Text3配置SublimeREPL快捷键的方法(Python)

    因为用sublime运行python,如果有input()函数,ctrl+b是不能输入数据的,所以下载安装了sublimeREPL进行调试. 但是sublimeREPL没有自定义快捷键,所以只有自己设 ...

  2. “echo >”和“echo >>”的区别

    > 输出重定向 >> 输出追加重定向 ------------------------------------------------------------------------ ...

  3. linq使用 count与sum等

    using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; us ...

  4. 限制MYSQL从服务器为只读状态

    修改全局变量的方法有两种,第一种是修改配置文件,第二种是SQL语句设置全局变量的值.(可以参考:http://www.cnblogs.com/qlqwjy/p/8046592.html) 0.简介: ...

  5. C++11中的小细节--字符串的原始字面量

    原始字面量很容易理解,即不进行转义的完整字符串. 最近看了看Python,其中讲到了原始字符串. Both string and bytes literals may optionally be pr ...

  6. windows编程中的数据类型

    在windows编程中,有许多奇怪的数据类型,初学者不知道这些代表什么,下面就把一些数据类型列出如下: ATOM 原子(原子表中的一个字符串的参考) BOOL 布尔变量 BOOLEAN 布尔变量 BY ...

  7. java的unity单元测试

    import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestUnit { publ ...

  8. 【原创】Talend ETL开发——基于joblet的统一的email发送

    更新 2019/1/16:增加了允许5个附件发送.smtp等配置的字段. 一.背景 在ETL数据整合过程中,肯定会涉及到email的通知,比如ETL的执行情况汇报,执行耗时汇报,关键数据更新情况等信息 ...

  9. 中矿大新生赛 A 求解位数和【字符串】

    时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言65536K64bit IO Format: %lld 题目描述 给出一个数x,求x的所有位数的和. 输入描述: 第 ...

  10. (寒假集训) Piggyback(最短路)

    Piggyback 时间限制: 1 Sec  内存限制: 64 MB提交: 3  解决: 3[提交][状态][讨论版] 题目描述 Bessie and her sister Elsie graze i ...