[Algorithms] Refactor a Linear Search into a Binary Search with JavaScript
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的更多相关文章
- 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 作者 ...
- 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 作者 ...
- 【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; ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Unique Binary Search Trees,Unique Binary Search Trees II
Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given ...
- 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 ...
随机推荐
- ServletContext结合Servlet接口中的init()方法和destroy()方法的运用----网站计数器
我们一般知道Servlet接口中的init()方法在tomcat启动时调用,destroy()方法在tomcat关闭时调用.那么这两个方法到底在实际开发中有什么作用呢?这就是这个随笔主要讲的内容. 思 ...
- 在ESXi使用esxcli命令強制关闭VM
最近學到一個在VMware ESXi 下面強制關閉一個沒有反應的VM的方法, 一般正常都是使用vSphere Client 去控制VM電源, 但是有時會發生即使用裡面的Power Off 按鈕但是還是 ...
- HDU5187 zhx's contest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission ...
- css3文件树
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- C++嵌套类及对外围类成员变量的访问
C++嵌套类及对外围类成员变量的访问 在一个类中定义的类称为嵌套类,定义嵌套类的类称为外围类. 定义嵌套类的目的在于隐藏类名,减少全局的标识符,从而限制用户能否使用该类建立对象.这样可以提高类的抽象能 ...
- logrotate 学习使用
logrotate Summary : Rotates, compresses, removes and mails system log files Description : The logrot ...
- hdu 5145(莫队算法+逆元)
NPY and girls Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- Android视频压缩并且上传
一,做视频上传首先第一步要从相册里选取视频,这一步.我的上篇博客中已经讲过了. protected void onActivityResult(int requestCode, int resultC ...
- AC日记——[Wc2008]游览计划 bzoj 2595
2595 思路: 状压DP+spfa转移+dfs输出路径: 或者,斯坦纳树算法模板: 来,上代码: #include <queue> #include <cstdio> #in ...
- NYOJ16 矩形嵌套 【DAG上的DP/LIS】
矩形嵌套 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c ...