time-to-botec

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

README.md (7526B)


      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 # dsort2sh
     22 
     23 > Simultaneously sort two double-precision floating-point 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 dsort2sh = require( '@stdlib/blas/ext/base/dsort2sh' );
     31 ```
     32 
     33 #### dsort2sh( N, order, x, strideX, y, strideY )
     34 
     35 Simultaneously sorts two double-precision floating-point strided arrays based on the sort order of the first array `x` using Shellsort.
     36 
     37 ```javascript
     38 var Float64Array = require( '@stdlib/array/float64' );
     39 
     40 var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );
     41 var y = new Float64Array( [ 0.0, 1.0, 2.0, 3.0 ] );
     42 
     43 dsort2sh( x.length, 1.0, x, 1, y, 1 );
     44 
     45 console.log( x );
     46 // => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]
     47 
     48 console.log( y );
     49 // => <Float64Array>[ 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 [`Float64Array`][@stdlib/array/float64].
     57 -   **strideX**: `x` index increment.
     58 -   **y**: second input [`Float64Array`][@stdlib/array/float64].
     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 Float64Array = require( '@stdlib/array/float64' );
     65 var floor = require( '@stdlib/math/base/special/floor' );
     66 
     67 var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );
     68 var y = new Float64Array( [ 0.0, 1.0, 2.0, 3.0 ] );
     69 var N = floor( x.length / 2 );
     70 
     71 dsort2sh( N, -1.0, x, 2, y, 2 );
     72 
     73 console.log( x );
     74 // => <Float64Array>[ 3.0, -2.0, 1.0, -4.0 ]
     75 
     76 console.log( y );
     77 // => <Float64Array>[ 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 Float64Array = require( '@stdlib/array/float64' );
     84 var floor = require( '@stdlib/math/base/special/floor' );
     85 
     86 // Initial arrays...
     87 var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
     88 var y0 = new Float64Array( [ 0.0, 1.0, 2.0, 3.0 ] );
     89 
     90 // Create offset views...
     91 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
     92 var y1 = new Float64Array( 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 dsort2sh( N, -1.0, x1, 2, y1, 2 );
     97 
     98 console.log( x0 );
     99 // => <Float64Array>[ 1.0, 4.0, 3.0, 2.0 ]
    100 
    101 console.log( y0 );
    102 // => <Float64Array>[ 0.0, 3.0, 2.0, 1.0 ]
    103 ```
    104 
    105 #### dsort2sh.ndarray( N, order, x, strideX, offsetX, y, strideY, offsetY )
    106 
    107 Simultaneously sorts two double-precision floating-point strided arrays based on the sort order of the first array `x` using Shellsort and alternative indexing semantics.
    108 
    109 ```javascript
    110 var Float64Array = require( '@stdlib/array/float64' );
    111 
    112 var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );
    113 var y = new Float64Array( [ 0.0, 1.0, 2.0, 3.0 ] );
    114 
    115 dsort2sh.ndarray( x.length, 1.0, x, 1, 0, y, 1, 0 );
    116 
    117 console.log( x );
    118 // => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]
    119 
    120 console.log( y );
    121 // => <Float64Array>[ 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 Float64Array = require( '@stdlib/array/float64' );
    133 
    134 var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
    135 var y = new Float64Array( [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ] );
    136 
    137 dsort2sh.ndarray( 3, 1.0, x, 1, x.length-3, y, 1, y.length-3 );
    138 
    139 console.log( x );
    140 // => <Float64Array>[ 1.0, -2.0, 3.0, -6.0, -4.0, 5.0 ]
    141 
    142 console.log( y );
    143 // => <Float64Array>[ 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 worst case time complexity `O(N^(4/3))`.
    158 -   The algorithm is efficient for **shorter** strided arrays (typically `N <= 50`).
    159 -   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).
    160 -   The input strided arrays are sorted **in-place** (i.e., the input strided arrays are **mutated**).
    161 
    162 </section>
    163 
    164 <!-- /.notes -->
    165 
    166 <section class="examples">
    167 
    168 ## Examples
    169 
    170 <!-- eslint no-undef: "error" -->
    171 
    172 ```javascript
    173 var round = require( '@stdlib/math/base/special/round' );
    174 var randu = require( '@stdlib/random/base/randu' );
    175 var Float64Array = require( '@stdlib/array/float64' );
    176 var dsort2sh = require( '@stdlib/blas/ext/base/dsort2sh' );
    177 
    178 var rand;
    179 var sign;
    180 var x;
    181 var y;
    182 var i;
    183 
    184 x = new Float64Array( 10 );
    185 y = new Float64Array( 10 ); // index array
    186 for ( i = 0; i < x.length; i++ ) {
    187     rand = round( randu()*100.0 );
    188     sign = randu();
    189     if ( sign < 0.5 ) {
    190         sign = -1.0;
    191     } else {
    192         sign = 1.0;
    193     }
    194     x[ i ] = sign * rand;
    195     y[ i ] = i;
    196 }
    197 console.log( x );
    198 console.log( y );
    199 
    200 dsort2sh( x.length, -1.0, x, -1, y, -1 );
    201 console.log( x );
    202 console.log( y );
    203 ```
    204 
    205 </section>
    206 
    207 <!-- /.examples -->
    208 
    209 * * *
    210 
    211 <section class="references">
    212 
    213 ## References
    214 
    215 -   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].
    216 -   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].
    217 -   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].
    218 
    219 </section>
    220 
    221 <!-- /.references -->
    222 
    223 <section class="links">
    224 
    225 [@stdlib/array/float64]: https://www.npmjs.com/package/@stdlib/array-float64
    226 
    227 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    228 
    229 [@shell:1959a]: https://doi.org/10.1145/368370.368387
    230 
    231 [@sedgewick:1986a]: https://doi.org/10.1016/0196-6774(86)90001-5
    232 
    233 [@ciura:2001a]: https://doi.org/10.1007/3-540-44669-9_12
    234 
    235 </section>
    236 
    237 <!-- /.links -->