Here's a quicksort routine that I (iirc) adapted slightly from one
posted here by Tony Gravagno...
SUBROUTINE SR.QSORT(MAT KEYS, MAT EXTRAS, START.AT, END.AT,
ASCENDING)
*
************************************************************************
*
*
* Arguments:
*
* KEYS(n) (I/O) Array of keys to be sorted
* EXTRAS(n) (I/O) Array of corresponding data
* START.AT (I) Sort from this position - usually 1
* END.AT (I) Sort to this position - usually n
* ASCENDING (I) Boolean for ascending or descending sort order
*
************************************************************************
*
*
DIM KEYS(1)
DIM EXTRAS(1)
*
************************************************************************
*
*
PTR.1 = START.AT
PTR.2 = END.AT
TEST.VALUE = KEYS((START.AT + END.AT) / 2)
*
LOOP
IF ASCENDING
THEN
LOOP
WHILE PTR.1 < END.AT AND TEST.VALUE > KEYS(PTR.1)
PTR.1 += 1
REPEAT
*
LOOP
WHILE PTR.2 > START.AT AND TEST.VALUE < KEYS(PTR.2)
PTR.2 -= 1
REPEAT
END
ELSE
LOOP
WHILE PTR.1 < END.AT AND TEST.VALUE < KEYS(PTR.1)
PTR.1 += 1
REPEAT
*
LOOP
WHILE PTR.2 > START.AT AND TEST.VALUE > KEYS(PTR.2)
PTR.2 -= 1
REPEAT
END
*
* Swap elements if required
*
IF PTR.1 < PTR.2
THEN
TEMP.1 = KEYS(PTR.1)
TEMP.2 = EXTRAS(PTR.1)
*
KEYS(PTR.1) = KEYS(PTR.2)
EXTRAS(PTR.1) = EXTRAS(PTR.2)
*
KEYS(PTR.2) = TEMP.1
EXTRAS(PTR.2) = TEMP.2
END
*
IF PTR.1 <= PTR.2
THEN
PTR.1 += 1
PTR.2 -= 1
END
WHILE PTR.1 <= PTR.2
REPEAT
*
* Sort remainder of the elements
*
IF START.AT < PTR.2
THEN CALL SR.QSORT(MAT KEYS, MAT EXTRAS, START.AT, PTR.2,
ASCENDING)
*
IF PTR.1 < END.AT
THEN CALL SR.QSORT(MAT KEYS, MAT EXTRAS, PTR.1, END.AT,
ASCENDING)
*
RETURN
*
****************************************
*
END
DISCLAIMER:
Disclaimer. This e-mail is private and confidential. If you are not the
intended recipient, please advise us by return e-mail immediately, and delete
the e-mail and any attachments without using or disclosing the contents in any
way. The views expressed in this e-mail are those of the author, and do not
represent those of this company unless this is clearly indicated. You should
scan this e-mail and any attachments for viruses. This company accepts no
liability for any direct or indirect damage or loss resulting from the use of
any attachments to this e-mail.
-------
u2-users mailing list
[email protected]
To unsubscribe please visit http://listserver.u2ug.org/