partitionSelect.md (1271B)
1 <!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. --> 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 ### Throws 33 34 Type | Description 35 ---- | ----------- 36 37 38 ## Examples 39 40 ```js 41 math.partitionSelect([5, 10, 1], 2) // returns 10 42 math.partitionSelect(['C', 'B', 'A', 'D'], 1) // returns 'B' 43 44 function sortByLength (a, b) { 45 return a.length - b.length 46 } 47 math.partitionSelect(['Langdon', 'Tom', 'Sara'], 2, sortByLength) // returns 'Langdon' 48 ``` 49 50 51 ## See also 52 53 [sort](sort.md)