{Function: _heapsort_a by Alex Matulich, 2/2004 Adapted from _Numerical Recipes in C_ This function sorts an array ra[] from index 0 to N. For large arrays, this is significantly faster than TradeStation's builtin SortUp function. } inputs: ra[m](NumericArrayRef), {array to be sorted} N(NumericSimple); {highest array index} vars: NN(0), ii(0), ir(0), jj(0), ll(0), rra(0), lp1(true), lp2(true); if m < N then NN = m else NN = N; if NN > 0 then begin ll = IntPortion(0.5*NN); ir = NN; while lp1 begin if ll > 0 then begin ll = ll-1; rra = ra[ll]; end else begin rra = ra[ir]; ra[ir] = ra[0]; ir = ir-1; if ir = 0 then begin ra[0] = rra; lp1 = false; end; end; ii = ll; jj = ll+ll; lp2 = true; while lp1 and lp2 and jj <= ir begin if jj < ir and ra[jj] < ra[jj+1] then {< =ascending, > =descending sort} jj = jj+1; if rra < ra[jj] then begin {< =ascending, > =descending sort} ra[ii] = ra[jj]; ii = jj; jj = jj+jj; end else lp2 = false; end; ra[ii] = rra; end; end; _heapsort_a = NN; {dummy return value}