|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2026 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# dsort |
| 22 | + |
| 23 | +> Sort a double-precision floating-point strided array. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var dsort = require( '@stdlib/blas/ext/base/dsort' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### dsort( N, order, x, strideX ) |
| 34 | + |
| 35 | +Sorts a double-precision floating-point strided array. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 39 | + |
| 40 | +var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); |
| 41 | + |
| 42 | +dsort( x.length, 1.0, x, 1 ); |
| 43 | +// x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ] |
| 44 | +``` |
| 45 | + |
| 46 | +The function has the following parameters: |
| 47 | + |
| 48 | +- **N**: number of indexed elements. |
| 49 | +- **order**: sort order. If `order < 0.0`, the input strided array is sorted in **decreasing** order. If `order > 0.0`, the input strided array is sorted in **increasing** order. If `order == 0.0`, the input strided array is left unchanged. |
| 50 | +- **x**: input [`Float64Array`][@stdlib/array/float64]. |
| 51 | +- **strideX**: stride length. |
| 52 | + |
| 53 | +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to sort every other element: |
| 54 | + |
| 55 | +```javascript |
| 56 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 57 | + |
| 58 | +var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); |
| 59 | + |
| 60 | +dsort( 2, -1.0, x, 2 ); |
| 61 | +// x => <Float64Array>[ 3.0, -2.0, 1.0, -4.0 ] |
| 62 | +``` |
| 63 | + |
| 64 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 65 | + |
| 66 | +```javascript |
| 67 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 68 | + |
| 69 | +// Initial array... |
| 70 | +var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); |
| 71 | + |
| 72 | +// Create an offset view... |
| 73 | +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 74 | + |
| 75 | +// Sort every other element... |
| 76 | +dsort( 2, -1.0, x1, 2 ); |
| 77 | +// x0 => <Float64Array>[ 1.0, 4.0, 3.0, 2.0 ] |
| 78 | +``` |
| 79 | + |
| 80 | +#### dsort.ndarray( N, order, x, strideX, offsetX ) |
| 81 | + |
| 82 | +Sorts a double-precision floating-point strided array using alternative indexing semantics. |
| 83 | + |
| 84 | +```javascript |
| 85 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 86 | + |
| 87 | +var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); |
| 88 | + |
| 89 | +dsort.ndarray( x.length, 1.0, x, 1, 0 ); |
| 90 | +// x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ] |
| 91 | +``` |
| 92 | + |
| 93 | +The function has the following additional parameters: |
| 94 | + |
| 95 | +- **offsetX**: starting index. |
| 96 | + |
| 97 | +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to access only the last three elements: |
| 98 | + |
| 99 | +```javascript |
| 100 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 101 | + |
| 102 | +var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); |
| 103 | + |
| 104 | +dsort.ndarray( 3, 1.0, x, 1, 3 ); |
| 105 | +// x => <Float64Array>[ 1.0, -2.0, 3.0, -6.0, -4.0, 5.0 ] |
| 106 | +``` |
| 107 | + |
| 108 | +</section> |
| 109 | + |
| 110 | +<!-- /.usage --> |
| 111 | + |
| 112 | +<section class="notes"> |
| 113 | + |
| 114 | +## Notes |
| 115 | + |
| 116 | +- If `N <= 0` or `order == 0.0`, both functions return `x` unchanged. |
| 117 | +- The algorithm distinguishes between `-0` and `+0`. When sorted in increasing order, `-0` is sorted before `+0`. When sorted in decreasing order, `-0` is sorted after `+0`. |
| 118 | +- The algorithm sorts `NaN` values to the end. When sorted in increasing order, `NaN` values are sorted last. When sorted in decreasing order, `NaN` values are sorted first. |
| 119 | +- The algorithm has space complexity `O(1)` and time complexity `O(N log2 N)`. |
| 120 | +- The input strided array is sorted **in-place** (i.e., the input strided array is **mutated**). |
| 121 | + |
| 122 | +</section> |
| 123 | + |
| 124 | +<!-- /.notes --> |
| 125 | + |
| 126 | +<section class="examples"> |
| 127 | + |
| 128 | +## Examples |
| 129 | + |
| 130 | +<!-- eslint no-undef: "error" --> |
| 131 | + |
| 132 | +```javascript |
| 133 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 134 | +var dsort = require( '@stdlib/blas/ext/base/dsort' ); |
| 135 | + |
| 136 | +var x = discreteUniform( 10, -100, 100, { |
| 137 | + 'dtype': 'float64' |
| 138 | +}); |
| 139 | +console.log( x ); |
| 140 | + |
| 141 | +dsort( x.length, 1.0, x, 1 ); |
| 142 | +console.log( x ); |
| 143 | +``` |
| 144 | + |
| 145 | +</section> |
| 146 | + |
| 147 | +<!-- /.examples --> |
| 148 | + |
| 149 | +<!-- C interface documentation. --> |
| 150 | + |
| 151 | +* * * |
| 152 | + |
| 153 | +<section class="c"> |
| 154 | + |
| 155 | +## C APIs |
| 156 | + |
| 157 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 158 | + |
| 159 | +<section class="intro"> |
| 160 | + |
| 161 | +</section> |
| 162 | + |
| 163 | +<!-- /.intro --> |
| 164 | + |
| 165 | +<!-- C usage documentation. --> |
| 166 | + |
| 167 | +<section class="usage"> |
| 168 | + |
| 169 | +### Usage |
| 170 | + |
| 171 | +```c |
| 172 | +#include "stdlib/blas/ext/base/dsort.h" |
| 173 | +``` |
| 174 | + |
| 175 | +#### stdlib_strided_dsort( N, order, \*X, strideX ) |
| 176 | + |
| 177 | +Sorts a double-precision floating-point strided array. |
| 178 | + |
| 179 | +```c |
| 180 | +const double x[] = { 1.0, 2.0, 3.0, 4.0 }; |
| 181 | + |
| 182 | +stdlib_strided_dsort( 4, 1.0, x, 1 ); |
| 183 | +``` |
| 184 | +
|
| 185 | +The function accepts the following arguments: |
| 186 | +
|
| 187 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 188 | +- **order**: `[in] double` sort order. If `order < 0.0`, the input strided array is sorted in **decreasing** order. If `order > 0.0`, the input strided array is sorted in **increasing** order. If `order == 0.0`, the input strided array is left unchanged. |
| 189 | +- **X**: `[inout] double*` input array. |
| 190 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 191 | +
|
| 192 | +```c |
| 193 | +void stdlib_strided_dsort( const CBLAS_INT N, const double order, double *X, const CBLAS_INT strideX ); |
| 194 | +``` |
| 195 | + |
| 196 | +<!--lint disable maximum-heading-length--> |
| 197 | + |
| 198 | +#### stdlib_strided_dsort_ndarray( N, order, \*X, strideX, offsetX ) |
| 199 | + |
| 200 | +<!--lint enable maximum-heading-length--> |
| 201 | + |
| 202 | +Sorts a double-precision floating-point strided array using alternative indexing semantics. |
| 203 | + |
| 204 | +```c |
| 205 | +const double x[] = { 1.0, 2.0, 3.0, 4.0 }; |
| 206 | + |
| 207 | +stdlib_strided_dsort_ndarray( 4, 1.0, x, 1, 0 ); |
| 208 | +``` |
| 209 | +
|
| 210 | +The function accepts the following arguments: |
| 211 | +
|
| 212 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 213 | +- **order**: `[in] double` sort order. If `order < 0.0`, the input strided array is sorted in **decreasing** order. If `order > 0.0`, the input strided array is sorted in **increasing** order. If `order == 0.0`, the input strided array is left unchanged. |
| 214 | +- **X**: `[inout] double*` input array. |
| 215 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 216 | +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. |
| 217 | +
|
| 218 | +```c |
| 219 | +void stdlib_strided_dsort_ndarray( const CBLAS_INT N, const double order, double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); |
| 220 | +``` |
| 221 | + |
| 222 | +</section> |
| 223 | + |
| 224 | +<!-- /.usage --> |
| 225 | + |
| 226 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 227 | + |
| 228 | +<section class="notes"> |
| 229 | + |
| 230 | +</section> |
| 231 | + |
| 232 | +<!-- /.notes --> |
| 233 | + |
| 234 | +<!-- C API usage examples. --> |
| 235 | + |
| 236 | +<section class="examples"> |
| 237 | + |
| 238 | +### Examples |
| 239 | + |
| 240 | +```c |
| 241 | +#include "stdlib/blas/ext/base/dsort.h" |
| 242 | +#include <stdio.h> |
| 243 | + |
| 244 | +int main( void ) { |
| 245 | + // Create a strided array: |
| 246 | + double x[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 }; |
| 247 | + |
| 248 | + // Specify the number of elements: |
| 249 | + const int N = 8; |
| 250 | + |
| 251 | + // Specify the stride length: |
| 252 | + const int strideX = 1; |
| 253 | + |
| 254 | + // Sort the array: |
| 255 | + stdlib_strided_dsort( N, 1.0, x, strideX ); |
| 256 | + |
| 257 | + // Print the result: |
| 258 | + for ( int i = 0; i < 8; i++ ) { |
| 259 | + printf( "x[ %i ] = %lf\n", i, x[ i ] ); |
| 260 | + } |
| 261 | +} |
| 262 | +``` |
| 263 | +
|
| 264 | +</section> |
| 265 | +
|
| 266 | +<!-- /.examples --> |
| 267 | +
|
| 268 | +</section> |
| 269 | +
|
| 270 | +<!-- /.c --> |
| 271 | +
|
| 272 | +<section class="references"> |
| 273 | +
|
| 274 | +</section> |
| 275 | +
|
| 276 | +<!-- /.references --> |
| 277 | +
|
| 278 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 279 | +
|
| 280 | +<section class="related"> |
| 281 | +
|
| 282 | +</section> |
| 283 | +
|
| 284 | +<!-- /.related --> |
| 285 | +
|
| 286 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 287 | +
|
| 288 | +<section class="links"> |
| 289 | +
|
| 290 | +[@stdlib/array/float64]: https://github.com/stdlib-js/array-float64 |
| 291 | +
|
| 292 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 293 | +
|
| 294 | +<!-- <related-links> --> |
| 295 | +
|
| 296 | +<!-- </related-links> --> |
| 297 | +
|
| 298 | +</section> |
| 299 | +
|
| 300 | +<!-- /.links --> |
0 commit comments