floyd-rivest
Advanced tools
Comparing version
{ | ||
"name": "floyd-rivest", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "The Floyd-Rivest k-smallest selection algorithm.", | ||
"license": "MIT", | ||
"main": "dist/floyd-rivest.js", | ||
"types": "dist/floyd-rivest.d.ts", | ||
"main": "bin/index.js", | ||
"files": [ | ||
"dist/**/*" | ||
"bin/index.js", | ||
"bin/index.d.ts" | ||
], | ||
"scripts": { | ||
"prepare": "npm run build", | ||
"build": "node ./node_modules/typescript/bin/tsc" | ||
"test": "mocha -r ts-node/register test/**/*-test.ts", | ||
"build": "tsc", | ||
"lint": "eslint . --ext .ts --fix", | ||
"minify": "jsmin -o bin/index.min.js bin/index.js && del bin\\index.js && move bin\\index.min.js bin\\index.js", | ||
"prepare": "tsc && npm test && npm run minify" | ||
}, | ||
@@ -30,4 +33,14 @@ "repository": { | ||
"devDependencies": { | ||
"typescript": "^3.3.3333" | ||
"@types/chai": "^4.2.14", | ||
"@types/mocha": "^8.0.4", | ||
"@types/node": "^18.11.9", | ||
"@typescript-eslint/eslint-plugin": "^4.10.0", | ||
"@typescript-eslint/parser": "^4.10.0", | ||
"chai": "^4.2.0", | ||
"eslint": "^7.15.0", | ||
"jsmin": "^1.0.1", | ||
"mocha": "^8.2.1", | ||
"ts-node": "^9.1.1", | ||
"typescript": "^3.9.7" | ||
} | ||
} |
# floyd-rivest | ||
A typescript of the Floyd-Rivest selection algorithm. | ||
A typescript implementation of the Floyd-Rivest selection algorithm. | ||
This module exports a single function, `select<T>(array: T[], k: number, compare?: (a: T, b: T) => number) => T` implementing the [Floyd-Rivest selection algorithm](https://en.wikipedia.org/wiki/Floyd%E2%80%93Rivest_algorithm). `select` returns the k-smallest element of the input array, and mutates the array to ensure that the k-smallest element is at index k-1, all items at indices < (k-1) are less than or equal to array[k-1] and all items at indices > (k-1) are greater than or equal to array[k-1]. Elements are not sorted within each partition. | ||
Usage | ||
===== | ||
``` | ||
const { select } = require('floyd-rivest'); | ||
const array = [1,6,2,3,9,4,7,2,7,0,5]; | ||
const e = select(array, 3); // e = 2, the third-smallest element of the array | ||
const ksmallest = array.slice(0,3); // ksmallest contains [0, 1, 2], not necessarily in that order | ||
``` |
15
400%3267
-45.4%11
1000%4
-20%12
-79.66%