%%HP: T(3)A(D)F(.);
@
@ General Bubble Sort Routine
@ 
@ input : lst l oflag pdir prg
@
@           lst : the list to be sorted.  Genrally, it should be
@                 a list of real number.  But it can be a list of list.
@                 see the input parameter l below.
@           l :  0 - if lst is a list of elements.
@                non 0 - when lst is a list of list, l specifies the element
@                number of each list within lst to be used for the sorting
@           oflag: sort order 
@                0 - ascending order
@                1 - descending order
@           pdir : if pdir \=/ {}, it specifies the directory where the
@                specified program is to be called to perform a function
@                on the selected element before sorting.
@           prg : optional program to be ran for each element before sorting.
@  output: lst : the sorted list 
@  Note: lst can not be an empty list
@
\<< {HOME URPLUI} \-> l oflag pdir prg udir \<<

DUP SIZE 1 - 1 FOR j
  1 j FOR k
    k GETI IF l THEN DUP l GET ELSE 0 SWAP END \-> ele1 n1 \<<
      GETI IF l THEN DUP l GET ELSE 0 SWAP END \-> ele2 n2 \<<
        DROP
        IF pdir {} \=/ THEN pdir EVAL n1 prg EVAL udir EVAL ELSE n1 END
        IF pdir {} \=/ THEN pdir EVAL n2 prg EVAL udir EVAL ELSE n2 END
        IF oflag THEN
           IF < THEN
             k IF l THEN ele2 ELSE n2 END PUTI
             IF l THEN ele1 ELSE n1 END PUT
           END
        ELSE
           IF > THEN
             k IF l THEN ele2 ELSE n2 END PUTI
             IF l THEN ele1 ELSE n1 END PUT
           END
        END
      \>>
    \>>
  NEXT
-1 STEP

\>>
\>>

