Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mourner/quickselect
A fast selection algorithm in JavaScript.
https://github.com/mourner/quickselect
algorithm floyd-rivest javascript quickselect selection sort
Last synced: 6 days ago
JSON representation
A fast selection algorithm in JavaScript.
- Host: GitHub
- URL: https://github.com/mourner/quickselect
- Owner: mourner
- License: isc
- Created: 2016-02-17T12:42:17.000Z (almost 9 years ago)
- Default Branch: main
- Last Pushed: 2024-09-24T09:16:48.000Z (4 months ago)
- Last Synced: 2025-01-18T03:02:22.452Z (13 days ago)
- Topics: algorithm, floyd-rivest, javascript, quickselect, selection, sort
- Language: JavaScript
- Size: 67.4 KB
- Stars: 87
- Watchers: 4
- Forks: 17
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## quickselect [![Node](https://github.com/mourner/quickselect/actions/workflows/node.yml/badge.svg)](https://github.com/mourner/quickselect/actions/workflows/node.yml) [![Simply Awesome](https://img.shields.io/badge/simply-awesome-brightgreen.svg)](https://github.com/mourner/projects)
A tiny and fast [selection algorithm](https://en.wikipedia.org/wiki/Selection_algorithm) in JavaScript
(specifically, [Floyd-Rivest selection](https://en.wikipedia.org/wiki/Floyd%E2%80%93Rivest_algorithm)).```js
quickselect(array, k[, left, right, compareFn]);
```Rearranges items so that all items in the `[left, k]` are the smallest.
The `k`-th element will have the `(k - left + 1)`-th smallest value in `[left, right]`.- `array`: the array to partially sort (in place)
- `k`: middle index for partial sorting (as defined above)
- `left`: left index of the range to sort (`0` by default)
- `right`: right index (last index of the array by default)
- `compareFn`: compare functionExample:
```js
const arr = [65, 28, 59, 33, 21, 56, 22, 95, 50, 12, 90, 53, 28, 77, 39];quickselect(arr, 8);
// arr is [39, 28, 28, 33, 21, 12, 22, 50, 53, 56, 59, 65, 90, 77, 95]
// ^^ middle index
```