UNPKG

1.22 kBMarkdownView Raw
1<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
2
3# Function partitionSelect
4
5Partition-based selection of an array or 1D matrix.
6Will find the kth smallest value, and mutates the input array.
7Uses Quickselect.
8
9
10## Syntax
11
12```js
13math.partitionSelect(x, k)
14math.partitionSelect(x, k, compare)
15```
16
17### Parameters
18
19Parameter | Type | Description
20--------- | ---- | -----------
21`x` | Matrix &#124; Array | A one dimensional matrix or array to sort
22`k` | Number | The kth smallest value to be retrieved zero-based index
23`compare` | Function &#124; 'asc' &#124; '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'.
24
25### Returns
26
27Type | Description
28---- | -----------
29* | Returns the kth lowest value.
30
31
32## Examples
33
34```js
35math.partitionSelect([5, 10, 1], 2) // returns 10
36math.partitionSelect(['C', 'B', 'A', 'D'], 1) // returns 'B'
37
38function sortByLength (a, b) {
39 return a.length - b.length
40}
41math.partitionSelect(['Langdon', 'Tom', 'Sara'], 2, sortByLength) // returns 'Langdon'
42```
43
44
45## See also
46
47[sort](sort.md)
48
\No newline at end of file