Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
}

Expand All @@ -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;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 15 additions & 9 deletions tests/bisection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};