time-to-botec

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

README.md (7242B)


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