Skip to content

Commit 19d5fc4

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/gsort
PR-URL: #9712 Closes: stdlib-js/metr-issue-tracker#119 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent b1ccc8c commit 19d5fc4

16 files changed

Lines changed: 3294 additions & 0 deletions

File tree

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
# gsort
22+
23+
> Sort a strided array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var gsort = require( '@stdlib/blas/ext/base/gsort' );
31+
```
32+
33+
#### gsort( N, order, x, strideX )
34+
35+
Sorts a strided array.
36+
37+
```javascript
38+
var x = [ 1.0, -2.0, 3.0, -4.0 ];
39+
40+
gsort( x.length, 1.0, x, 1 );
41+
// x => [ -4.0, -2.0, 1.0, 3.0 ]
42+
```
43+
44+
The function has the following parameters:
45+
46+
- **N**: number of indexed elements.
47+
- **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.
48+
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
49+
- **strideX**: stride length.
50+
51+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to sort every other element:
52+
53+
```javascript
54+
var x = [ 1.0, -2.0, 3.0, -4.0 ];
55+
56+
gsort( 2, -1.0, x, 2 );
57+
// x => [ 3.0, -2.0, 1.0, -4.0 ]
58+
```
59+
60+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
61+
62+
```javascript
63+
var Float64Array = require( '@stdlib/array/float64' );
64+
65+
// Initial array...
66+
var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
67+
68+
// Create an offset view...
69+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
70+
71+
// Sort every other element...
72+
gsort( 2, -1.0, x1, 2 );
73+
// x0 => <Float64Array>[ 1.0, 4.0, 3.0, 2.0 ]
74+
```
75+
76+
#### gsort.ndarray( N, order, x, strideX, offsetX )
77+
78+
Sorts a strided array using alternative indexing semantics.
79+
80+
```javascript
81+
var x = [ 1.0, -2.0, 3.0, -4.0 ];
82+
83+
gsort.ndarray( x.length, 1.0, x, 1, 0 );
84+
// x => [ -4.0, -2.0, 1.0, 3.0 ]
85+
```
86+
87+
The function has the following additional parameters:
88+
89+
- **offsetX**: starting index.
90+
91+
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:
92+
93+
```javascript
94+
var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ];
95+
96+
gsort.ndarray( 3, 1.0, x, 1, x.length-3 );
97+
// x => [ 1.0, -2.0, 3.0, -6.0, -4.0, 5.0 ]
98+
```
99+
100+
</section>
101+
102+
<!-- /.usage -->
103+
104+
<section class="notes">
105+
106+
## Notes
107+
108+
- If `N <= 0` or `order == 0.0`, both functions return `x` unchanged.
109+
- 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`.
110+
- 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.
111+
- The input strided array is sorted **in-place** (i.e., the input strided array is **mutated**).
112+
113+
</section>
114+
115+
<!-- /.notes -->
116+
117+
<section class="examples">
118+
119+
## Examples
120+
121+
<!-- eslint no-undef: "error" -->
122+
123+
```javascript
124+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
125+
var gsort = require( '@stdlib/blas/ext/base/gsort' );
126+
127+
var x = discreteUniform( 10, -100, 100, {
128+
'dtype': 'generic'
129+
});
130+
console.log( x );
131+
132+
gsort( x.length, -1.0, x, -1 );
133+
console.log( x );
134+
```
135+
136+
</section>
137+
138+
<!-- /.examples -->
139+
140+
<section class="references">
141+
142+
</section>
143+
144+
<!-- /.references -->
145+
146+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
147+
148+
<section class="related">
149+
150+
</section>
151+
152+
<!-- /.related -->
153+
154+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
155+
156+
<section class="links">
157+
158+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
159+
160+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
161+
162+
<!-- <related-links> -->
163+
164+
<!-- </related-links> -->
165+
166+
</section>
167+
168+
<!-- /.links -->
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var floor = require( '@stdlib/math/base/special/floor' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var gsort = require( './../lib/main.js' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Create a benchmark function.
37+
*
38+
* @private
39+
* @param {PositiveInteger} iter - number of iterations
40+
* @param {PositiveInteger} len - array length
41+
* @returns {Function} benchmark function
42+
*/
43+
function createBenchmark( iter, len ) {
44+
var tmp;
45+
var x;
46+
var i;
47+
48+
x = [];
49+
for ( i = 0; i < iter; i++ ) {
50+
tmp = uniform( len, -100.0, 100.0, {
51+
'dtype': 'generic'
52+
});
53+
x.push( tmp );
54+
}
55+
return benchmark;
56+
57+
/**
58+
* Benchmark function.
59+
*
60+
* @private
61+
* @param {Benchmark} b - benchmark instance
62+
*/
63+
function benchmark( b ) {
64+
var xc;
65+
var y;
66+
var i;
67+
68+
xc = x.slice();
69+
for ( i = 0; i < iter; i++ ) {
70+
xc[ i ] = x[ i ].slice();
71+
}
72+
b.tic();
73+
for ( i = 0; i < b.iterations; i++ ) {
74+
y = gsort( len, 1, xc[ i ], 1 );
75+
if ( isnan( y[ i%len ] ) ) {
76+
b.fail( 'should not return NaN' );
77+
}
78+
}
79+
b.toc();
80+
if ( isnan( y[ i%len ] ) ) {
81+
b.fail( 'should not return NaN' );
82+
}
83+
b.pass( 'benchmark finished' );
84+
b.end();
85+
}
86+
}
87+
88+
89+
// MAIN //
90+
91+
function main() {
92+
var opts;
93+
var iter;
94+
var len;
95+
var min;
96+
var max;
97+
var f;
98+
var i;
99+
100+
iter = 1e6;
101+
min = 1; // 10^min
102+
max = 4; // 10^max
103+
104+
for ( i = min; i <= max; i++ ) {
105+
len = pow( 10, i );
106+
f = createBenchmark( iter, len );
107+
opts = {
108+
'iterations': iter
109+
};
110+
bench( format( '%s::unsorted,random:len=%d', pkg, len ), opts, f );
111+
iter = floor( pow( iter, 3.0/4.0 ) );
112+
}
113+
}
114+
115+
main();

0 commit comments

Comments
 (0)