time-to-botec

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

README.md (7404B)


      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 # gsort2sh
     22 
     23 > Simultaneously sort two strided arrays based on the sort order of the first array using Shellsort.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var gsort2sh = require( '@stdlib/blas/ext/base/gsort2sh' );
     31 ```
     32 
     33 #### gsort2sh( N, order, x, strideX, y, strideY )
     34 
     35 Simultaneously sorts two strided arrays based on the sort order of the first array `x` using Shellsort.
     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 gsort2sh( 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 gsort2sh( 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 gsort2sh( 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 #### gsort2sh.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 Shellsort 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 gsort2sh.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 gsort2sh.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^(4/3))`.
    151 -   The algorithm is efficient for **shorter** strided arrays (typically `N <= 50`).
    152 -   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).
    153 -   The input strided arrays are sorted **in-place** (i.e., the input strided arrays are **mutated**).
    154 -   Depending on the environment, the typed versions ([`dsort2sh`][@stdlib/blas/ext/base/dsort2sh], [`ssort2sh`][@stdlib/blas/ext/base/ssort2sh], 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 gsort2sh = require( '@stdlib/blas/ext/base/gsort2sh' );
    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 gsort2sh( 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 -   Shell, Donald L. 1959. "A High-Speed Sorting Procedure." _Communications of the ACM_ 2 (7). Association for Computing Machinery: 30–32. doi:[10.1145/368370.368387][@shell:1959a].
    210 -   Sedgewick, Robert. 1986. "A new upper bound for Shellsort." _Journal of Algorithms_ 7 (2): 159–73. doi:[10.1016/0196-6774(86)90001-5][@sedgewick:1986a].
    211 -   Ciura, Marcin. 2001. "Best Increments for the Average Case of Shellsort." In _Fundamentals of Computation Theory_, 106–17. Springer Berlin Heidelberg. doi:[10.1007/3-540-44669-9_12][@ciura:2001a].
    212 
    213 </section>
    214 
    215 <!-- /.references -->
    216 
    217 <section class="links">
    218 
    219 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
    220 
    221 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    222 
    223 [@stdlib/blas/ext/base/dsort2sh]: https://www.npmjs.com/package/@stdlib/blas/tree/main/ext/base/dsort2sh
    224 
    225 [@stdlib/blas/ext/base/ssort2sh]: https://www.npmjs.com/package/@stdlib/blas/tree/main/ext/base/ssort2sh
    226 
    227 [@shell:1959a]: https://doi.org/10.1145/368370.368387
    228 
    229 [@sedgewick:1986a]: https://doi.org/10.1016/0196-6774(86)90001-5
    230 
    231 [@ciura:2001a]: https://doi.org/10.1007/3-540-44669-9_12
    232 
    233 </section>
    234 
    235 <!-- /.links -->