mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
63 lines
1.9 KiB
Markdown
63 lines
1.9 KiB
Markdown
---
|
|
layout: default
|
|
---
|
|
|
|
<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
|
|
|
|
<h1 id="function-partitionselect">Function partitionSelect <a href="#function-partitionselect" title="Permalink">#</a></h1>
|
|
|
|
Partition-based selection of an array or 1D matrix.
|
|
Will find the kth smallest value, and mutates the input array.
|
|
Uses Quickselect.
|
|
|
|
|
|
<h2 id="syntax">Syntax <a href="#syntax" title="Permalink">#</a></h2>
|
|
|
|
```js
|
|
math.partitionSelect(x, k)
|
|
math.partitionSelect(x, k, compare)
|
|
```
|
|
|
|
<h3 id="parameters">Parameters <a href="#parameters" title="Permalink">#</a></h3>
|
|
|
|
Parameter | Type | Description
|
|
--------- | ---- | -----------
|
|
`x` | Matrix | Array | A one dimensional matrix or array to sort
|
|
`k` | Number | The kth smallest value to be retrieved zero-based index
|
|
`compare` | Function | 'asc' | 'desc' | An optional comparator function. The function is called as `compare(a, b)`, and must return 1 when a > b, -1 when a < b, and 0 when a == b. Default value: 'asc'.
|
|
|
|
<h3 id="returns">Returns <a href="#returns" title="Permalink">#</a></h3>
|
|
|
|
Type | Description
|
|
---- | -----------
|
|
* | Returns the kth lowest value.
|
|
|
|
|
|
<h3 id="throws">Throws <a href="#throws" title="Permalink">#</a></h3>
|
|
|
|
Type | Description
|
|
---- | -----------
|
|
|
|
|
|
<h2 id="examples">Examples <a href="#examples" title="Permalink">#</a></h2>
|
|
|
|
```js
|
|
math.partitionSelect([5, 10, 1], 2) // returns 10
|
|
math.partitionSelect(['C', 'B', 'A', 'D'], 1, math.compareText) // returns 'B'
|
|
|
|
function sortByLength (a, b) {
|
|
return a.length - b.length
|
|
}
|
|
math.partitionSelect(['Langdon', 'Tom', 'Sara'], 2, sortByLength) // returns 'Langdon'
|
|
|
|
// the input array is mutated
|
|
arr = [5, 2, 1]
|
|
math.partitionSelect(arr, 0) // returns 1, arr is now: [1, 2, 5]
|
|
math.partitionSelect(arr, 1, 'desc') // returns 2, arr is now: [5, 2, 1]
|
|
```
|
|
|
|
|
|
<h2 id="see-also">See also <a href="#see-also" title="Permalink">#</a></h2>
|
|
|
|
[sort](sort.html)
|