Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/atlassubbed/atlas-median


https://github.com/atlassubbed/atlas-median

Last synced: 21 days ago
JSON representation

Awesome Lists containing this project

README

        

# atlas-median

Calculates the median of a set of data points in-place.

[![Travis](https://img.shields.io/travis/atlassubbed/atlas-median.svg)](https://travis-ci.org/atlassubbed/atlas-median)

---

## install

```
npm install --save atlas-median
```

## why

Breaking up [atlas-dataset](https://github.com/atlassubbed/atlas-dataset#readme) into standalone functions. This module computes the median value over an array of numbers:


median(V) = (v[ceil(|V|/2)] + v[ceil((|V|+1)/2)])/2

## examples

#### unsorted array

The `median` function sorts the array in-place before taking the middle value. In the case of an even-length array, the median is the mean of the two middle values.

```javascript
const median = require("atlas-median")
console.log(median([4,3,1,2]))
// 2.5
```

#### sorted array

To avoid sorting a pre-sorted array, use a boolean flag:

```javascript
...
const isSorted = true;
console.log(median([1,2,3,4,5], isSorted)) // fast
// 3
```