Computes the median of an array.
npm install compute-medianMedian
===
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependencies][dependencies-image]][dependencies-url]
> Computes the median of an array.
`` bash`
$ npm install compute-median
For use in the browser, use browserify.
` javascript`
var median = require( 'compute-median' );
#### median( arr[, options] )
Computes the median of an array. For unsorted primitive number arrays,
` javascript
var unsorted = [ 5, 3, 2, 4 ];
var m1 = median( unsorted );
// returns 3.5
`
The function accepts two options:
* sorted: boolean flag indicating if the input array is sorted in __ascending__ order. Default: false.accessor
* : accessor function for accessing values in object arrays.
If the input array is already sorted in __ascending__ order, set the sorted option to true.
` javascript
var sorted = [ 2, 3, 4, 5 ];
var m2 = median( sorted, {
'sorted': true,
});
// returns 3.5
`
For object arrays, provide an accessor function for accessing numeric array values
` javascript
var data = [
[1,5],
[3,3],
[4,2],
[5,4],
];
function getValue( d ) {
return d[ 1 ];
}
var m3 = median( data, {
'sorted': false,
'accessor': getValue
});
// returns 3.5
`
__Note__: if provided an empty array, the function returns null.
` javascript
var median = require( 'compute-median' );
var data = new Array( 1001 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = Math.round( Math.random() * 100 );
}
console.log( median( data ) );
`
To run the example code from the top-level application directory,
` bash`
$ node ./examples/index.js
For an input array of length N,
* if provided a sorted (in __ascending__ order) numeric array, the function is O(1).array
* if provided a sorted object , the function is O(N).array
* if provided an unsorted numeric , the function is O( N log(N) ).array
* if provided an unsorted object , the function is O( N + N log(N) ).
Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
` bash`
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
` bash`
$ make test-cov
Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,
` bash``
$ make view-cov
---
Copyright © 2014-2015. Athan Reines.
[npm-image]: http://img.shields.io/npm/v/compute-median.svg
[npm-url]: https://npmjs.org/package/compute-median
[travis-image]: http://img.shields.io/travis/compute-io/median/master.svg
[travis-url]: https://travis-ci.org/compute-io/median
[coveralls-image]: https://img.shields.io/coveralls/compute-io/median/master.svg
[coveralls-url]: https://coveralls.io/r/compute-io/median?branch=master
[dependencies-image]: http://img.shields.io/david/compute-io/median.svg
[dependencies-url]: https://david-dm.org/compute-io/median
[dev-dependencies-image]: http://img.shields.io/david/dev/compute-io/median.svg
[dev-dependencies-url]: https://david-dm.org/dev/compute-io/median
[github-issues-image]: http://img.shields.io/github/issues/compute-io/median.svg
[github-issues-url]: https://github.com/compute-io/median/issues