time-to-botec

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

README.md (5498B)


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