time-to-botec

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

README.md (5888B)


      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 # dsorthp
     22 
     23 > Sort a double-precision floating-point strided array using heapsort.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var dsorthp = require( '@stdlib/blas/ext/base/dsorthp' );
     31 ```
     32 
     33 #### dsorthp( N, order, x, stride )
     34 
     35 Sorts a double-precision floating-point strided array `x` using heapsort.
     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 
     42 dsorthp( x.length, 1.0, x, 1 );
     43 // x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]
     44 ```
     45 
     46 The function has the following parameters:
     47 
     48 -   **N**: number of indexed elements.
     49 -   **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.
     50 -   **x**: input [`Float64Array`][@stdlib/array/float64].
     51 -   **stride**: index increment.
     52 
     53 The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to sort every other element
     54 
     55 ```javascript
     56 var Float64Array = require( '@stdlib/array/float64' );
     57 var floor = require( '@stdlib/math/base/special/floor' );
     58 
     59 var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );
     60 var N = floor( x.length / 2 );
     61 
     62 dsorthp( N, -1.0, x, 2 );
     63 // x => <Float64Array>[ 3.0, -2.0, 1.0, -4.0 ]
     64 ```
     65 
     66 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
     67 
     68 ```javascript
     69 var Float64Array = require( '@stdlib/array/float64' );
     70 var floor = require( '@stdlib/math/base/special/floor' );
     71 
     72 // Initial array...
     73 var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
     74 
     75 // Create an offset view...
     76 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
     77 var N = floor( x0.length/2 );
     78 
     79 // Sort every other element...
     80 dsorthp( N, -1.0, x1, 2 );
     81 // x0 => <Float64Array>[ 1.0, 4.0, 3.0, 2.0 ]
     82 ```
     83 
     84 #### dsorthp.ndarray( N, order, x, stride, offset )
     85 
     86 Sorts a double-precision floating-point strided array `x` using heapsort and alternative indexing semantics.
     87 
     88 ```javascript
     89 var Float64Array = require( '@stdlib/array/float64' );
     90 
     91 var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );
     92 
     93 dsorthp.ndarray( x.length, 1.0, x, 1, 0 );
     94 // x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]
     95 ```
     96 
     97 The function has the following additional parameters:
     98 
     99 -   **offset**: starting index.
    100 
    101 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`
    102 
    103 ```javascript
    104 var Float64Array = require( '@stdlib/array/float64' );
    105 
    106 var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
    107 
    108 dsorthp.ndarray( 3, 1.0, x, 1, x.length-3 );
    109 // x => <Float64Array>[ 1.0, -2.0, 3.0, -6.0, -4.0, 5.0 ]
    110 ```
    111 
    112 </section>
    113 
    114 <!-- /.usage -->
    115 
    116 <section class="notes">
    117 
    118 ## Notes
    119 
    120 -   If `N <= 0` or `order == 0.0`, both functions return `x` unchanged.
    121 -   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`.
    122 -   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.
    123 -   The algorithm has space complexity `O(1)` and time complexity `O(N log2 N)`.
    124 -   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).
    125 -   The input strided array is sorted **in-place** (i.e., the input strided array is **mutated**).
    126 
    127 </section>
    128 
    129 <!-- /.notes -->
    130 
    131 <section class="examples">
    132 
    133 ## Examples
    134 
    135 <!-- eslint no-undef: "error" -->
    136 
    137 ```javascript
    138 var round = require( '@stdlib/math/base/special/round' );
    139 var randu = require( '@stdlib/random/base/randu' );
    140 var Float64Array = require( '@stdlib/array/float64' );
    141 var dsorthp = require( '@stdlib/blas/ext/base/dsorthp' );
    142 
    143 var rand;
    144 var sign;
    145 var x;
    146 var i;
    147 
    148 x = new Float64Array( 10 );
    149 for ( i = 0; i < x.length; i++ ) {
    150     rand = round( randu()*100.0 );
    151     sign = randu();
    152     if ( sign < 0.5 ) {
    153         sign = -1.0;
    154     } else {
    155         sign = 1.0;
    156     }
    157     x[ i ] = sign * rand;
    158 }
    159 console.log( x );
    160 
    161 dsorthp( x.length, -1.0, x, -1 );
    162 console.log( x );
    163 ```
    164 
    165 </section>
    166 
    167 <!-- /.examples -->
    168 
    169 * * *
    170 
    171 <section class="references">
    172 
    173 ## References
    174 
    175 -   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].
    176 -   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].
    177 
    178 </section>
    179 
    180 <!-- /.references -->
    181 
    182 <section class="links">
    183 
    184 [@stdlib/array/float64]: https://www.npmjs.com/package/@stdlib/array-float64
    185 
    186 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    187 
    188 [@williams:1964a]: https://doi.org/10.1145/512274.512284
    189 
    190 [@floyd:1964a]: https://doi.org/10.1145/355588.365103
    191 
    192 </section>
    193 
    194 <!-- /.links -->