'============================================================================== ' ' Example code for sorting a virtual long integer array ' Copyright (c) 1997 by PowerBASIC, Inc. All Rights Reserved. ' ' This code can easily be adapted to other data types. ' '============================================================================== $STACK 4096 IF FRE(-11) = 0 THEN PRINT "EMS not available for virtual arrays" END END IF DIM i AS LONG DIM u AS LONG u = 5000 DIM VIRTUAL x(u) AS LONG PRINT "Filling array..." FOR i = 0 TO u x(i) = RND(0, u) NEXT i PRINT "Sorting array..." SortVlong x() FOR i = 0 TO u PRINT x(i) NEXT i '------------------------------------------------------------------------------ ' Sort a string array using a quick-sort. Works with both standard and ' virtual arrays. Calls a recursive quicksort sub to do the actual work. ' SUB SortVlong(vArray(1) AS LONG) PUBLIC DIM l AS LONG DIM u AS LONG l = LBOUND(vArray(1)) u = UBOUND(vArray(1)) QSort vArray(), l, u END SUB '------------------------------------------------------------------------------ ' Recursive QuickSort ' SUB QSort(vArray(1) AS LONG, lowbound AS LONG, hibound AS LONG) PRIVATE DIM low AS LONG DIM high AS LONG DIM midval AS LONG DIM temp AS LONG low = lowbound high = hibound midval = vArray((low + high) / 2) WHILE (low <= high) WHILE vArray(low) < midval AND low < hibound INCR low WEND WHILE midval < vArray(high) AND high > lowbound DECR high WEND IF (low <= high) THEN ' SWAP statement is not supported with virtual arrays temp = vArray(low) vArray(low) = vArray(High) vArray(high) = temp INCR low DECR high END IF WEND IF (lowbound < high) THEN QSort vArray(), lowbound, high END IF IF (low < hibound) THEN QSort vArray(), low, hibound END IF END SUB