Skip to content

Commit 3c7f8d2

Browse files
headlessNodekgrytestdlib-bot
authored
feat: add blas/ext/base/ssort
PR-URL: #9697 Closes: stdlib-js/metr-issue-tracker#118 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent 7a08720 commit 3c7f8d2

35 files changed

+5328
-0
lines changed
Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
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+
# ssort
22+
23+
> Sort a single-precision floating-point strided array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var ssort = require( '@stdlib/blas/ext/base/ssort' );
31+
```
32+
33+
#### ssort( N, order, x, strideX )
34+
35+
Sorts a single-precision floating-point strided array.
36+
37+
```javascript
38+
var Float32Array = require( '@stdlib/array/float32' );
39+
40+
var x = new Float32Array( [ 1.0, -2.0, 3.0, -4.0 ] );
41+
42+
ssort( x.length, 1.0, x, 1 );
43+
// x => <Float32Array>[ -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 [`Float32Array`][@stdlib/array/float32].
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 Float32Array = require( '@stdlib/array/float32' );
57+
58+
var x = new Float32Array( [ 1.0, -2.0, 3.0, -4.0 ] );
59+
60+
ssort( 2, -1.0, x, 2 );
61+
// x => <Float32Array>[ 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 Float32Array = require( '@stdlib/array/float32' );
68+
69+
// Initial array...
70+
var x0 = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] );
71+
72+
// Create an offset view...
73+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
74+
75+
// Sort every other element...
76+
ssort( 2, -1.0, x1, 2 );
77+
// x0 => <Float32Array>[ 1.0, 4.0, 3.0, 2.0 ]
78+
```
79+
80+
#### ssort.ndarray( N, order, x, strideX, offsetX )
81+
82+
Sorts a single-precision floating-point strided array using alternative indexing semantics.
83+
84+
```javascript
85+
var Float32Array = require( '@stdlib/array/float32' );
86+
87+
var x = new Float32Array( [ 1.0, -2.0, 3.0, -4.0 ] );
88+
89+
ssort.ndarray( x.length, 1.0, x, 1, 0 );
90+
// x => <Float32Array>[ -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 Float32Array = require( '@stdlib/array/float32' );
101+
102+
var x = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
103+
104+
ssort.ndarray( 3, 1.0, x, 1, 3 );
105+
// x => <Float32Array>[ 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 input strided array is sorted **in-place** (i.e., the input strided array is **mutated**).
120+
121+
</section>
122+
123+
<!-- /.notes -->
124+
125+
<section class="examples">
126+
127+
## Examples
128+
129+
<!-- eslint no-undef: "error" -->
130+
131+
```javascript
132+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
133+
var ssort = require( '@stdlib/blas/ext/base/ssort' );
134+
135+
var x = discreteUniform( 10, -100, 100, {
136+
'dtype': 'float32'
137+
});
138+
console.log( x );
139+
140+
ssort( x.length, 1.0, x, 1 );
141+
console.log( x );
142+
```
143+
144+
</section>
145+
146+
<!-- /.examples -->
147+
148+
<!-- C interface documentation. -->
149+
150+
* * *
151+
152+
<section class="c">
153+
154+
## C APIs
155+
156+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
157+
158+
<section class="intro">
159+
160+
</section>
161+
162+
<!-- /.intro -->
163+
164+
<!-- C usage documentation. -->
165+
166+
<section class="usage">
167+
168+
### Usage
169+
170+
```c
171+
#include "stdlib/blas/ext/base/ssort.h"
172+
```
173+
174+
#### stdlib_strided_ssort( N, order, \*X, strideX )
175+
176+
Sorts a single-precision floating-point strided array.
177+
178+
```c
179+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
180+
181+
stdlib_strided_ssort( 4, 1.0f, x, 1 );
182+
```
183+
184+
The function accepts the following arguments:
185+
186+
- **N**: `[in] CBLAS_INT` number of indexed elements.
187+
- **order**: `[in] float` 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.
188+
- **X**: `[inout] float*` input array.
189+
- **strideX**: `[in] CBLAS_INT` stride length.
190+
191+
```c
192+
void stdlib_strided_ssort( const CBLAS_INT N, const float order, float *X, const CBLAS_INT strideX );
193+
```
194+
195+
<!--lint disable maximum-heading-length-->
196+
197+
#### stdlib_strided_ssort_ndarray( N, order, \*X, strideX, offsetX )
198+
199+
<!--lint enable maximum-heading-length-->
200+
201+
Sorts a single-precision floating-point strided array using alternative indexing semantics.
202+
203+
```c
204+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
205+
206+
stdlib_strided_ssort_ndarray( 4, 1.0f, x, 1, 0 );
207+
```
208+
209+
The function accepts the following arguments:
210+
211+
- **N**: `[in] CBLAS_INT` number of indexed elements.
212+
- **order**: `[in] float` 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.
213+
- **X**: `[inout] float*` input array.
214+
- **strideX**: `[in] CBLAS_INT` stride length.
215+
- **offsetX**: `[in] CBLAS_INT` starting index.
216+
217+
```c
218+
void stdlib_strided_ssort_ndarray( const CBLAS_INT N, const float order, float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
219+
```
220+
221+
</section>
222+
223+
<!-- /.usage -->
224+
225+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
226+
227+
<section class="notes">
228+
229+
</section>
230+
231+
<!-- /.notes -->
232+
233+
<!-- C API usage examples. -->
234+
235+
<section class="examples">
236+
237+
### Examples
238+
239+
```c
240+
#include "stdlib/blas/ext/base/ssort.h"
241+
#include <stdio.h>
242+
243+
int main( void ) {
244+
// Create a strided array:
245+
float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f };
246+
247+
// Specify the number of elements:
248+
const int N = 8;
249+
250+
// Specify the stride length:
251+
const int strideX = 1;
252+
253+
// Sort the array:
254+
stdlib_strided_ssort( N, 1.0f, x, strideX );
255+
256+
// Print the result:
257+
for ( int i = 0; i < 8; i++ ) {
258+
printf( "x[ %i ] = %f\n", i, x[ i ] );
259+
}
260+
}
261+
```
262+
263+
</section>
264+
265+
<!-- /.examples -->
266+
267+
</section>
268+
269+
<!-- /.c -->
270+
271+
<section class="references">
272+
273+
</section>
274+
275+
<!-- /.references -->
276+
277+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
278+
279+
<section class="related">
280+
281+
</section>
282+
283+
<!-- /.related -->
284+
285+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
286+
287+
<section class="links">
288+
289+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
290+
291+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
292+
293+
<!-- <related-links> -->
294+
295+
<!-- </related-links> -->
296+
297+
</section>
298+
299+
<!-- /.links -->

0 commit comments

Comments
 (0)