1 |
|
2 |
|
3 | # Function partitionSelect
|
4 |
|
5 | Partition-based selection of an array or 1D matrix.
|
6 | Will find the kth smallest value, and mutates the input array.
|
7 | Uses Quickselect.
|
8 |
|
9 |
|
10 | ## Syntax
|
11 |
|
12 | ```js
|
13 | math.partitionSelect(x, k)
|
14 | math.partitionSelect(x, k, compare)
|
15 | ```
|
16 |
|
17 | ### Parameters
|
18 |
|
19 | Parameter | Type | Description
|
20 | --------- | ---- | -----------
|
21 | `x` | Matrix | 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 | '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'.
|
24 |
|
25 | ### Returns
|
26 |
|
27 | Type | Description
|
28 | ---- | -----------
|
29 | * | Returns the kth lowest value.
|
30 |
|
31 |
|
32 | ## Examples
|
33 |
|
34 | ```js
|
35 | math.partitionSelect([5, 10, 1], 2) // returns 10
|
36 | math.partitionSelect(['C', 'B', 'A', 'D'], 1) // returns 'B'
|
37 |
|
38 | function sortByLength (a, b) {
|
39 | return a.length - b.length
|
40 | }
|
41 | math.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 |