|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# dsortsh |
| 22 | + |
| 23 | +> Sort a one-dimensional double-precision floating-point ndarray using Shellsort. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +</section> |
| 28 | + |
| 29 | +<!-- /.intro --> |
| 30 | + |
| 31 | +<section class="usage"> |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```javascript |
| 36 | +var dsortsh = require( '@stdlib/blas/ext/base/ndarray/dsortsh' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### dsortsh( arrays ) |
| 40 | + |
| 41 | +Sorts a one-dimensional double-precision floating-point ndarray using Shellsort. |
| 42 | + |
| 43 | +```javascript |
| 44 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 45 | +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); |
| 46 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 47 | +var ndarray = require( '@stdlib/ndarray/base/ctor' ); |
| 48 | + |
| 49 | +var xbuf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); |
| 50 | +var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); |
| 51 | + |
| 52 | +var order = scalar2ndarray( 1.0, { |
| 53 | + 'dtype': 'generic' |
| 54 | +}); |
| 55 | + |
| 56 | +var out = dsortsh( [ x, order ] ); |
| 57 | +// returns <ndarray> |
| 58 | + |
| 59 | +var arr = ndarray2array( out ); |
| 60 | +// returns [ -4.0, -2.0, 1.0, 3.0 ] |
| 61 | +``` |
| 62 | + |
| 63 | +The function has the following parameters: |
| 64 | + |
| 65 | +- **arrays**: array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray specifying the sort order. |
| 66 | + |
| 67 | +</section> |
| 68 | + |
| 69 | +<!-- /.usage --> |
| 70 | + |
| 71 | +<section class="notes"> |
| 72 | + |
| 73 | +## Notes |
| 74 | + |
| 75 | +- The input ndarray is sorted **in-place** (i.e., the input ndarray is **mutated**). |
| 76 | +- When the sort order is less than zero, the input ndarray is sorted in **decreasing** order. When the sort order is greater than zero, the input ndarray is sorted in **increasing** order. When the sort order is equal to zero, the input ndarray is left unchanged. |
| 77 | +- 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`. |
| 78 | +- 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. |
| 79 | +- The algorithm has space complexity `O(1)` and worst case time complexity `O(N^(4/3))`. |
| 80 | +- The algorithm is efficient for **shorter** ndarrays (typically `N <= 50`). |
| 81 | + |
| 82 | +</section> |
| 83 | + |
| 84 | +<!-- /.notes --> |
| 85 | + |
| 86 | +<section class="examples"> |
| 87 | + |
| 88 | +## Examples |
| 89 | + |
| 90 | +<!-- eslint no-undef: "error" --> |
| 91 | + |
| 92 | +```javascript |
| 93 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 94 | +var ndarray = require( '@stdlib/ndarray/base/ctor' ); |
| 95 | +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); |
| 96 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 97 | +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); |
| 98 | +var dsortsh = require( '@stdlib/blas/ext/base/ndarray/dsortsh' ); |
| 99 | + |
| 100 | +var xbuf = discreteUniform( 10, -100, 100, { |
| 101 | + 'dtype': 'float64' |
| 102 | +}); |
| 103 | +var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); |
| 104 | +console.log( ndarray2array( x ) ); |
| 105 | + |
| 106 | +var order = scalar2ndarray( 1.0, { |
| 107 | + 'dtype': 'generic' |
| 108 | +}); |
| 109 | +console.log( 'Order:', ndarraylike2scalar( order ) ); |
| 110 | + |
| 111 | +dsortsh( [ x, order ] ); |
| 112 | +console.log( ndarray2array( x ) ); |
| 113 | +``` |
| 114 | + |
| 115 | +</section> |
| 116 | + |
| 117 | +<!-- /.examples --> |
| 118 | + |
| 119 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 120 | + |
| 121 | +<section class="related"> |
| 122 | + |
| 123 | +</section> |
| 124 | + |
| 125 | +<!-- /.related --> |
| 126 | + |
| 127 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 128 | + |
| 129 | +<section class="links"> |
| 130 | + |
| 131 | +</section> |
| 132 | + |
| 133 | +<!-- /.links --> |
0 commit comments