@stdlib/blas-ext-base-ndarray-dsortins
v0.1.1
Published
Sort a one-dimensional double-precision floating-point ndarray using insertion sort.
Readme
dsortins
Sort a one-dimensional double-precision floating-point ndarray using insertion sort.
Installation
npm install @stdlib/blas-ext-base-ndarray-dsortinsUsage
var dsortins = require( '@stdlib/blas-ext-base-ndarray-dsortins' );dsortins( arrays )
Sorts a one-dimensional double-precision floating-point ndarray using insertion sort.
var Float64Array = require( '@stdlib/array-float64' );
var scalar2ndarray = require( '@stdlib/ndarray-from-scalar' );
var ndarray2array = require( '@stdlib/ndarray-to-array' );
var ndarray = require( '@stdlib/ndarray-base-ctor' );
var xbuf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );
var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
var order = scalar2ndarray( 1.0, {
'dtype': 'generic'
});
var out = dsortins( [ x, order ] );
// returns <ndarray>
var arr = ndarray2array( out );
// returns [ -4.0, -2.0, 1.0, 3.0 ]The function has the following parameters:
- arrays: array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray specifying the sort order.
Notes
- The input ndarray is sorted in-place (i.e., the input ndarray is mutated).
- 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.
- The algorithm distinguishes between
-0and+0. When sorted in increasing order,-0is sorted before+0. When sorted in decreasing order,-0is sorted after+0. - The algorithm sorts
NaNvalues to the end. When sorted in increasing order,NaNvalues are sorted last. When sorted in decreasing order,NaNvalues are sorted first. - The algorithm has space complexity
O(1)and worst case time complexityO(N^2). - The algorithm is efficient for small ndarrays (typically
N <= 20) and is particularly efficient for sorting ndarrays which are already substantially sorted. - The algorithm is stable, meaning that the algorithm does not change the order of ndarray elements which are equal or equivalent (e.g.,
NaNvalues).
Examples
var discreteUniform = require( '@stdlib/random-array-discrete-uniform' );
var ndarray = require( '@stdlib/ndarray-base-ctor' );
var scalar2ndarray = require( '@stdlib/ndarray-from-scalar' );
var ndarray2array = require( '@stdlib/ndarray-to-array' );
var ndarraylike2scalar = require( '@stdlib/ndarray-base-ndarraylike2scalar' );
var dsortins = require( '@stdlib/blas-ext-base-ndarray-dsortins' );
var xbuf = discreteUniform( 10, -100, 100, {
'dtype': 'float64'
});
var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
console.log( ndarray2array( x ) );
var order = scalar2ndarray( 1.0, {
'dtype': 'generic'
});
console.log( 'Order:', ndarraylike2scalar( order ) );
dsortins( [ x, order ] );
console.log( ndarray2array( x ) );Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2026. The Stdlib Authors.
