/* ?? REXX Heap sort ?qx? Note that numeric values are sorted numerically, i.e. 0009 > 5 */ Call HeapSort 'text.' /* sort stem TEXT. */ . . . Heapsort: arg _heapn .;Return HeapSortX(_heapn) HeapsortX: Procedure expose (_heapn) /* Heapsort main */ arg _heapn . m = Value(_heapn'0') n = m do k=m % 2 to 1 by -1 call HeapSortD _heapn k n end do while n>1 t = Value(_heapn'1') zz=Value(_heapn'1',Value(_heapn||n)) zz=Value(_heapn||n,t) n = n-1 call HeapSortD _heapn 1 n end return 0 HeapSortD: Procedure expose (_heapn) /* downheap sub of HeapSort */ */ parse Arg _heapn k n v = Value(_heapn||k) do while k <= n%2 j = k+k if j < n then do i = j+1 if Value(_heapn||j) < Value(_heapn||i) then j=j+1 end if v >= Value(_heapn||j) then return Value(_heapn||k,v) zz=Value(_heapn||k,Value(_heapn||j)) k = j end return Value(_heapn||k,v)