Javascript Array binarysearch(i)
/**//from www . j av a2 s . co m * Project: geoipd * User: av4me * Main Repo: github.com/av4me/geoipd * Date: 04/05/2013 * License: MIT */ // inspiration: binarymax & James Khoury @ http://codereview.stackexchange.com/questions/1480/better-more-efficient-way-of-writing-this-javascript-binary-search Array.prototype.binarysearch = function (i) { var l = 0, u = this.length, m; while ( l <= u ) { // m = Math.floor((l+u)/2); m = ((l+u) >> 1); if ( i > this[m].i ) { l = m+1; } else { u = (i == this[m]) ? -2 : m - 1; } } return (u == -2) ? m : -1; };
var input = [ 1, 2, 2, 3, 4, 4, 5, 6, 8, 10 ]; Array.prototype.binarySearch = function(num) { var _this = this; function search(left, right) { if(right < left) return -1; else if(left === right) { return checkIndexForNum(left); }/*from www . ja va 2 s .com*/ var mid = Math.floor((left + right) / 2); if(num > _this[mid]) { return search(mid + 1, right); } else if(num < _this[mid]) { return search(left, mid - 1); } else { return checkIndexForNum(mid); } } function checkIndexForNum(index) { if(_this[index] === num) { return index; } else { return -1; } } return search(0, this.length - 1); }; console.log(input.binarySearch(-1)); console.log(input.binarySearch(8)); console.log(input.binarySearch(4)); console.log(input.binarySearch(0)); console.log(input.binarySearch(10000));
Array.prototype.binarySearch = function (searchElement) { 'use strict'; var minIndex = 0; var maxIndex = this.length - 1; var currentIndex; var currentElement; while (minIndex <= maxIndex) { currentIndex = (minIndex + maxIndex) / 2 | 0; currentElement = this[currentIndex]; if (currentElement < searchElement) { minIndex = currentIndex + 1; }/*from w w w .j a v a 2 s .c om*/ else if (currentElement > searchElement) { maxIndex = currentIndex - 1; } else { return currentIndex; } } return -1; }
/** Binary Search// w w w. j av a 2 s. com * Find the index of a value given. * O(log(n)) runtime. */ Array.prototype.binarySearch = function(target) { var low = 0; var high = this.length - 1; var middle; while (low <= high) { middle = Math.floor(((high + low) / 2)); if (this[middle] === target) { return middle } else if (this[middle] < target) { low = middle + 1; } else { high = middle - 1; } } return "Index not found"; }; // Test var testArray = [0, 6, 9, 12, 49, 50, 80, 800, 900]; testArray.binarySearch(900) // 8 testArray.binarySearch(10000) // "Index not found"
Array.prototype.binarySearch = function(val){ var min = 0;/*from w w w . j av a 2 s. c o m*/ var max = this.length -1; var currentIndex; var currentElement; while (min <= max) { currentIndex = Math.floor((min +max) / 2); currentElement = this[currentIndex]; if (currentElement > val) { max = currentIndex -1; } else if (currentElement < val) { min = currentIndex + 1; } else { return currentIndex; } } return -1; }; module.exports = { Array };
Array.prototype.binarySearch = function(value) { 'use strict'; if(!value){//from w w w . j ava 2 s. c o m return null; } var left = 0, right = this.length-1, find = false, middle = null; while( left <= right && !find ){ middle = parseInt(left+(right-left)/2); if(this[middle]<value){ left = middle + 1; } else if(this[middle] > value){ right = middle -1; } else { find = true; } } if(find){ return middle; } else { return false; } }; var vector = [1, 2, 3, 5, 6, 7, 9, 82]; console.log(vector); console.log(vector.binarySearch(7));
Array.prototype.binarySearch = function(value){ var items = this; console.log(items.length);/*from www .j a v a2 s.c om*/ var startIndex = 0; var stopIndex = items.length - 1; var middle = Math.floor((stopIndex + startIndex)/2); while(items[middle] != value && startIndex < stopIndex){ //adjust search area if (value < items[middle]){ stopIndex = middle - 1; } if (value > items[middle]){ startIndex = middle + 1; } //recalculate middle middle = Math.floor((stopIndex + startIndex)/2); } //make sure it's the right value return (items[middle] != value) ? console.log(-1) : console.log(middle); } sam = [6,7,8,5,3, 4, 2,9,1] sam.binarySearch(7);