time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (7224B)


      1 <!--
      2 
      3 @license Apache-2.0
      4 
      5 Copyright (c) 2020 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 # ssort2hp
     22 
     23 > Simultaneously sort two single-precision floating-point strided arrays based on the sort order of the first array using heapsort.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var ssort2hp = require( '@stdlib/blas/ext/base/ssort2hp' );
     31 ```
     32 
     33 #### ssort2hp( N, order, x, strideX, y, strideY )
     34 
     35 Simultaneously sorts two single-precision floating-point strided arrays based on the sort order of the first array `x` using heapsort.
     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 var y = new Float32Array( [ 0.0, 1.0, 2.0, 3.0 ] );
     42 
     43 ssort2hp( x.length, 1.0, x, 1, y, 1 );
     44 
     45 console.log( x );
     46 // => <Float32Array>[ -4.0, -2.0, 1.0, 3.0 ]
     47 
     48 console.log( y );
     49 // => <Float32Array>[ 3.0, 1.0, 0.0, 2.0 ]
     50 ```
     51 
     52 The function has the following parameters:
     53 
     54 -   **N**: number of indexed elements.
     55 -   **order**: sort order. If `order < 0.0`, the input strided array `x` is sorted in **decreasing** order. If `order > 0.0`, the input strided array `x` is sorted in **increasing** order. If `order == 0.0`, the input strided arrays are left unchanged.
     56 -   **x**: first input [`Float32Array`][@stdlib/array/float32].
     57 -   **strideX**: `x` index increment.
     58 -   **y**: second input [`Float32Array`][@stdlib/array/float32].
     59 -   **strideY**: `y` index increment.
     60 
     61 The `N` and `stride` parameters determine which elements in `x` and `y` are accessed at runtime. For example, to sort every other element
     62 
     63 ```javascript
     64 var Float32Array = require( '@stdlib/array/float32' );
     65 var floor = require( '@stdlib/math/base/special/floor' );
     66 
     67 var x = new Float32Array( [ 1.0, -2.0, 3.0, -4.0 ] );
     68 var y = new Float32Array( [ 0.0, 1.0, 2.0, 3.0 ] );
     69 var N = floor( x.length / 2 );
     70 
     71 ssort2hp( N, -1.0, x, 2, y, 2 );
     72 
     73 console.log( x );
     74 // => <Float32Array>[ 3.0, -2.0, 1.0, -4.0 ]
     75 
     76 console.log( y );
     77 // => <Float32Array>[ 2.0, 1.0, 0.0, 3.0 ]
     78 ```
     79 
     80 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
     81 
     82 ```javascript
     83 var Float32Array = require( '@stdlib/array/float32' );
     84 var floor = require( '@stdlib/math/base/special/floor' );
     85 
     86 // Initial arrays...
     87 var x0 = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] );
     88 var y0 = new Float32Array( [ 0.0, 1.0, 2.0, 3.0 ] );
     89 
     90 // Create offset views...
     91 var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
     92 var y1 = new Float32Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
     93 var N = floor( x0.length/2 );
     94 
     95 // Sort every other element...
     96 ssort2hp( N, -1.0, x1, 2, y1, 2 );
     97 
     98 console.log( x0 );
     99 // => <Float32Array>[ 1.0, 4.0, 3.0, 2.0 ]
    100 
    101 console.log( y0 );
    102 // => <Float32Array>[ 0.0, 3.0, 2.0, 1.0 ]
    103 ```
    104 
    105 #### ssort2hp.ndarray( N, order, x, strideX, offsetX, y, strideY, offsetY )
    106 
    107 Simultaneously sorts two single-precision floating-point strided arrays based on the sort order of the first array `x` using heapsort and alternative indexing semantics.
    108 
    109 ```javascript
    110 var Float32Array = require( '@stdlib/array/float32' );
    111 
    112 var x = new Float32Array( [ 1.0, -2.0, 3.0, -4.0 ] );
    113 var y = new Float32Array( [ 0.0, 1.0, 2.0, 3.0 ] );
    114 
    115 ssort2hp.ndarray( x.length, 1.0, x, 1, 0, y, 1, 0 );
    116 
    117 console.log( x );
    118 // => <Float32Array>[ -4.0, -2.0, 1.0, 3.0 ]
    119 
    120 console.log( y );
    121 // => <Float32Array>[ 3.0, 1.0, 0.0, 2.0 ]
    122 ```
    123 
    124 The function has the following additional parameters:
    125 
    126 -   **offsetX**: `x` starting index.
    127 -   **offsetY**: `y` starting index.
    128 
    129 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 of `x`
    130 
    131 ```javascript
    132 var Float32Array = require( '@stdlib/array/float32' );
    133 
    134 var x = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
    135 var y = new Float32Array( [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ] );
    136 
    137 ssort2hp.ndarray( 3, 1.0, x, 1, x.length-3, y, 1, y.length-3 );
    138 
    139 console.log( x );
    140 // => <Float32Array>[ 1.0, -2.0, 3.0, -6.0, -4.0, 5.0 ]
    141 
    142 console.log( y );
    143 // => <Float32Array>[ 0.0, 1.0, 2.0, 5.0, 3.0, 4.0 ]
    144 ```
    145 
    146 </section>
    147 
    148 <!-- /.usage -->
    149 
    150 <section class="notes">
    151 
    152 ## Notes
    153 
    154 -   If `N <= 0` or `order == 0.0`, both functions leave `x` and `y` unchanged.
    155 -   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`.
    156 -   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.
    157 -   The algorithm has space complexity `O(1)` and time complexity `O(N log2 N)`.
    158 -   The algorithm is **unstable**, meaning that the algorithm may change the order of strided array elements which are equal or equivalent (e.g., `NaN` values).
    159 -   The input strided arrays are sorted **in-place** (i.e., the input strided arrays are **mutated**).
    160 
    161 </section>
    162 
    163 <!-- /.notes -->
    164 
    165 <section class="examples">
    166 
    167 ## Examples
    168 
    169 <!-- eslint no-undef: "error" -->
    170 
    171 ```javascript
    172 var round = require( '@stdlib/math/base/special/round' );
    173 var randu = require( '@stdlib/random/base/randu' );
    174 var Float32Array = require( '@stdlib/array/float32' );
    175 var ssort2hp = require( '@stdlib/blas/ext/base/ssort2hp' );
    176 
    177 var rand;
    178 var sign;
    179 var x;
    180 var y;
    181 var i;
    182 
    183 x = new Float32Array( 10 );
    184 y = new Float32Array( 10 ); // index array
    185 for ( i = 0; i < x.length; i++ ) {
    186     rand = round( randu()*100.0 );
    187     sign = randu();
    188     if ( sign < 0.5 ) {
    189         sign = -1.0;
    190     } else {
    191         sign = 1.0;
    192     }
    193     x[ i ] = sign * rand;
    194     y[ i ] = i;
    195 }
    196 console.log( x );
    197 console.log( y );
    198 
    199 ssort2hp( x.length, -1.0, x, -1, y, -1 );
    200 console.log( x );
    201 console.log( y );
    202 ```
    203 
    204 </section>
    205 
    206 <!-- /.examples -->
    207 
    208 * * *
    209 
    210 <section class="references">
    211 
    212 ## References
    213 
    214 -   Williams, John William Joseph. 1964. "Algorithm 232: Heapsort." _Communications of the ACM_ 7 (6). New York, NY, USA: Association for Computing Machinery: 347–49. doi:[10.1145/512274.512284][@williams:1964a].
    215 -   Floyd, Robert W. 1964. "Algorithm 245: Treesort." _Communications of the ACM_ 7 (12). New York, NY, USA: Association for Computing Machinery: 701. doi:[10.1145/355588.365103][@floyd:1964a].
    216 
    217 </section>
    218 
    219 <!-- /.references -->
    220 
    221 <section class="links">
    222 
    223 [@stdlib/array/float32]: https://www.npmjs.com/package/@stdlib/array-float32
    224 
    225 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    226 
    227 [@williams:1964a]: https://doi.org/10.1145/512274.512284
    228 
    229 [@floyd:1964a]: https://doi.org/10.1145/355588.365103
    230 
    231 </section>
    232 
    233 <!-- /.links -->