diff --git a/README.md b/README.md index 2283f81..485d33b 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,11 @@ 1. [Node.js](http://nodejs.org/) 2. `npm install bisection` +# PROPERTIES + +1. half-inclusive, index of x = [ bisection.left(list, x) , bisection.right(list, x) ) +2. number of x in list = bisection.right(list, x) - bisection.left(list, x) + # UNIT TESTS ``` diff --git a/index.js b/index.js index 6d6f6e0..26dc083 100644 --- a/index.js +++ b/index.js @@ -37,6 +37,7 @@ bisection.right = bisection; /** * Calculates the index of the Array where item X should be placed, assuming the Array is sorted. + * * @param {Array} array The array containing the items. * @param {number} x The item that needs to be added to the array. * @param {number} low Inital Index that is used to start searching, optional. @@ -54,10 +55,10 @@ bisection.left = function left( array, x, low , high ){ while (low < high) { mid = (low + high) >> 1; - if (x < array[mid]) { - low = mid + 1; - } else { + if (x <= array[mid]) { high = mid; + } else { + low = mid + 1; } } @@ -67,6 +68,6 @@ bisection.left = function left( array, x, low , high ){ /** * Library version */ -bisection.version = '0.0.3'; +bisection.version = '0.0.4'; module.exports = bisection; diff --git a/package.json b/package.json index 1ae4e82..149b8fa 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "author": "Arnout Kazemier", "description": "A JavaScript port of the bisection algorithm that is used in Python", "scripts": { - "test": "node tests/bisection.test.js" + "test": "make test" }, "keywords": [ "algorithm", diff --git a/tests/bisection.test.js b/tests/bisection.test.js index 025303f..9278b27 100644 --- a/tests/bisection.test.js +++ b/tests/bisection.test.js @@ -21,16 +21,22 @@ module.exports = { bisection(large, 242).should.equal(4); bisection(large, 0).should.equal(0); bisection(large, 232424).should.equal(14); + } , 'Bisection left': function(){ - bisection.left(small, 4).should.equal(9); - bisection.left(small, 15).should.equal(0); - bisection.left(small, 4, 5).should.equal(9); - bisection.left(small, 12, 0, 5).should.equal(0); - - bisection.left(large, 350).should.equal(0); - bisection.left(large, 242).should.equal(14); - bisection.left(large, 0).should.equal(14); - bisection.left(large, 232424).should.equal(0); + bisection.left(small, 4).should.equal(4); + bisection.left(small, 15).should.equal(9); + bisection.left(small, 4, 5).should.equal(5); + bisection.left(small, 12, 0, 5).should.equal(5); + bisection.left(large, 350).should.equal(8); + bisection.left(large, 242).should.equal(4); + bisection.left(large, 0).should.equal(0); + bisection.left(large, 232424).should.equal(14); + } +, 'Bisection count': function(){ + (bisection.right(small, 3) - bisection.left(small, 3)).should.equal(1); + (bisection.right(small, 4) - bisection.left(small, 4)).should.equal(0); + (bisection.right(large, 122) - bisection.left(large, 122)).should.equal(1); + (bisection.right(large, 4) - bisection.left(large, 4)).should.equal(0); } };