All files dlasrt.js

50% Statements 125/250
68.75% Branches 11/16
100% Functions 1/1
50% Lines 125/250

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 2513x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x     4x 4x 2x 2x 4x     2x 2x 4x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 6x 6x 4x 4x 4x 6x 2x 2x 4x 4x 4x 4x 2x                                     2x                                                                                                                                                                                                           2x 2x 4x 3x 3x 3x 3x 3x  
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// MODULES //
 
var lowercase = require( '@stdlib/string/base/lowercase' );
var floor = require( '@stdlib/math/base/special/floor' );
var Float64Array = require( '@stdlib/array/float64' );
 
 
// VARIABLES //
 
var SELECT = 20;
 
 
// MAIN //
 
/**
* Sort an array of doubles in increasing or decreasing order using quicksort, with insertion sort for small partitions (size <= 20).
*
* @private
* @param {string} ID - sort direction: 'I' or 'D'
* @param {NonNegativeInteger} N - number of elements to sort
* @param {Float64Array} D - array to sort in-place
* @param {integer} strideD - stride length for `d`
* @param {NonNegativeInteger} offsetD - starting index for `d`
* @returns {integer} status code (0 = success, -i = the i-th argument had an illegal value)
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var D = new Float64Array( [ 5, 7, 9, 3, 11 ] );
*
* dlasrt( 'I', 5, D, 1, 0 );
* // D => <Float64Array>[ 3, 5, 7, 9, 11 ]
*/
function dlasrt( ID, N, D, strideD, offsetD ) {
	var stkpnt;
	var stack;
	var start;
	var dmnmx;
	var endd;
	var idx1;
	var idx2;
	var idx3;
	var dir;
	var tmp;
	var d1;
	var d2;
	var d3;
	var i;
	var j;
 
	// Determine sort direction
	dir = -1;
	if ( lowercase( ID ) === 'd' ) {
		dir = 0;
	} else if ( lowercase( ID ) === 'i' ) {
		dir = 1;
	}
 
	if ( dir === -1 ) {
		return -1;
	}
	if ( N < 0 ) {
		return -2;
	}
 
	// Quick return
	if ( N <= 1 ) {
		return 0;
	}
 
	// Initialize the stack with the full range (using 0-based indices)...
	stkpnt = 0;
	stack = new Float64Array( 64 );
	stack[ 0 ] = 0;
	stack[ 1 ] = N - 1;
 
	while ( stkpnt >= 0 ) {
		start = stack[ 2*stkpnt ];
		endd = stack[ ( 2*stkpnt ) + 1 ];
		stkpnt -= 1;
 
		if ( endd - start <= SELECT && endd - start > 0 ) {
			// Insertion sort for small partitions
			if ( dir === 0 ) {
				// Sort in decreasing order
				idx1 = offsetD + ( ( start + 1 )*strideD );
				for ( i = start + 1; i <= endd; i++ ) {
					idx2 = idx1; // index of i
					for ( j = i; j >= start + 1; j-- ) {
						idx3 = idx2 - strideD; // index of j - 1
						if ( D[ idx2 ] > D[ idx3 ] ) {
							dmnmx = D[ idx2 ];
							D[ idx2 ] = D[ idx3 ];
							D[ idx3 ] = dmnmx;
						} else {
							break;
						}
						idx2 -= strideD;
					}
					idx1 += strideD;
				}
			} else {
				// Sort in increasing order
				idx1 = offsetD + ( ( start + 1 )*strideD );
				for ( i = start + 1; i <= endd; i++ ) {
					idx2 = idx1; // index of i
					for ( j = i; j >= start + 1; j-- ) {
						idx3 = idx2 - strideD; // index of j - 1
						if ( D[ idx2 ] < D[ idx3 ] ) {
							dmnmx = D[ idx2 ];
							D[ idx2 ] = D[ idx3 ];
							D[ idx3 ] = dmnmx;
						} else {
							break;
						}
						idx2 -= strideD;
					}
					idx1 += strideD;
				}
			}
		} else if ( endd - start > SELECT ) {
			// Quicksort partition using median-of-three pivot
			d1 = D[ offsetD + ( start*strideD ) ];
			d2 = D[ offsetD + ( endd*strideD ) ];
			i = floor( ( start + endd ) / 2 );
			d3 = D[ offsetD + ( i*strideD ) ];

			// Find median of d1, d2, d3
			if ( d1 < d2 ) {
				if ( d3 < d1 ) {
					dmnmx = d1;
				} else if ( d3 < d2 ) {
					dmnmx = d3;
				} else {
					dmnmx = d2;
				}
			} else if ( d3 < d2 ) {
				dmnmx = d2;
			} else if ( d3 < d1 ) {
				dmnmx = d3;
			} else {
				dmnmx = d1;
			}

			if ( dir === 0 ) {
				// Partition for decreasing order
				i = start;
				j = endd;
				while ( true ) {
					idx1 = offsetD + ( j*strideD );
					while ( D[ idx1 ] < dmnmx ) {
						idx1 -= strideD;
						j -= 1;
					}

					idx2 = offsetD + ( i*strideD );
					while ( D[ idx2 ] > dmnmx ) {
						idx2 += strideD;
						i += 1;
					}
					if ( i < j ) {
						tmp = D[ idx2 ];
						D[ idx2 ] = D[ idx1 ];
						D[ idx1 ] = tmp;
						i += 1;
						j -= 1;
					} else {
						break;
					}
				}
			} else {
				// Partition for increasing order
				i = start;
				j = endd;
				while ( true ) {
					idx1 = offsetD + ( j*strideD );
					while ( D[ idx1 ] > dmnmx ) {
						idx1 -= strideD;
						j -= 1;
					}

					idx2 = offsetD + ( i*strideD );
					while ( D[ idx2 ] < dmnmx ) {
						idx2 += strideD;
						i += 1;
					}
					if ( i < j ) {
						tmp = D[ idx2 ];
						D[ idx2 ] = D[ idx1 ];
						D[ idx1 ] = tmp;
						i += 1;
						j -= 1;
					} else {
						break;
					}
				}
			}

			// Push sub-partitions onto stack (larger first for bounded stack depth)
			idx1 = 2*stkpnt;
			if ( j - start > endd - j - 1 ) {
				stkpnt += 1;
				idx1 += 2;
				stack[ idx1 ] = start;
				stack[ idx1 + 1 ] = j;

				stkpnt += 1;
				idx1 += 2;
				stack[ idx1 ] = j + 1;
				stack[ idx1 + 1 ] = endd;
			} else {
				stkpnt += 1;
				idx1 += 2;
				stack[ idx1 ] = j + 1;
				stack[ idx1 + 1 ] = endd;

				stkpnt += 1;
				idx1 += 2;
				stack[ idx1 ] = start;
				stack[ idx1 + 1 ] = j;
			}
		}
	}
	return 0;
}
 
 
// EXPORTS //
 
module.exports = dlasrt;