REM ... QUICKSORT ...
REM
REM ... WRITTEN BY: EDWARD A. VALENZUELA
REM                 JANUARY 25, 1982
REM
REM --------------------------------------------------------------------
REM
REM   VARIABLES:
REM		
REM	SL(P) ... THE LEFT END OF THE NEXT SUBLIST
REM     SR(P) ... THE RIGHT END OF THE NEXT SUBLIST
REM 	I ....... INDEX VARIABLE FOR THE LEFT SIDE OF LIST
REM     I1 ...... LEFT END OF THE CURRENT LIST
REM	J ....... INDEX VARIABLE FOR THE RIGHT SIDE OF LIST
REM	J1 ...... RIGHT END OF THE CURRENT LIST
REM 	S ....... DIRECTION SWITCH (-1 AT I, +1 AT J)
REM	T ....... TEMPORARY EXCHANGE VARIABLE
REM 	X( ) .... LIST OF NUMBERS TO BE SORTED
REM	P ....... POINTER TO THE TOP OF THE STACK OF SUBLISTS
REM	
REM --------------------------------------------------------------------

1500 REM
DIM SL%(20),SR%(20)

P% = 1
SL%(1) = 1
SR%(1) = N%

WHILE P%>0
  I1%=SL%(P%)
  J1%=SR%(P%)
  P%=P%-1
      WHILE I1% < J1%
        I% = I1% 
        J% = J1% 
        S% = -1 
            WHILE I% < J% 
              IF X%(KODE%(I%)) > X%(KODE%(J%)) THEN \
                T% = KODE%(I%) : \
                KODE%(I%) = KODE%(J%) : \
                KODE%(J%) = T% : \
              S% = SGN(-S%)
              IF S% = 1 THEN I%=I%+1 ELSE J%=J%-1
            WEND
        IF I%+1 < J1% THEN
          P% = P% + 1 : \
          SL%(P%) = I% + 1 : \
          SR%(P%) = J1%  
        J1% = I% - 1
      WEND
WEND

RETURN
