03-05-2003, 07:03 AM
I thought QB recursion was slower than using arrays.
However now it seems quite the opposite, after some testing.
I tested i%, j%, and k% arrays in my QB recursive quicksort. On a 5000-value array, the speed slows from about 4 to 5 seconds to 7.5 seconds, a speed decrease of 50%!!!
Sorry for confusing some people who saw my earlier post about making a non-recursive quicksort. I deleted it.
However now it seems quite the opposite, after some testing.
I tested i%, j%, and k% arrays in my QB recursive quicksort. On a 5000-value array, the speed slows from about 4 to 5 seconds to 7.5 seconds, a speed decrease of 50%!!!
Sorry for confusing some people who saw my earlier post about making a non-recursive quicksort. I deleted it.
Code:
DECLARE SUB do.sort (array1() AS INTEGER, g%, h%)
'initialize an array.
array.max% = 5000: DIM array1(array.max%) AS INTEGER, number AS INTEGER
'generate random numbers.
CLS : RANDOMIZE TIMER: FOR i% = 1 TO array.max%: array1(i%) = INT(RND * 5000) + 1: NEXT i%
'sort the array.
t1 = TIMER
do.sort array1(), 1, array.max%
t1 = TIMER - t1
PRINT the; sorted; array.
PRINT "Final sorted array :": PRINT : FOR i% = 1 TO array.max%: PRINT array1(i%); : NEXT i%
PRINT : PRINT : PRINT t1
SUB do.sort (array1() AS INTEGER, g%, h%)
IF g% >= h% THEN EXIT SUB
IF h% - g% < 1 THEN IF array1(g%) > array1(h%) THEN SWAP array1(g%), array1(h%): EXIT SUB ELSE GOTO 1
1 SWAP array1(h%), array1(INT(RND * (h% - g% + 1)) + g%): k% = array1(h%)
2 i% = g%: j% = h%
3 IF i% < j% AND array1(i%) <= k% THEN i% = i% + 1: GOTO 3
4 IF j% > i% AND array1(j%) >= k% THEN j% = j% - 1: GOTO 4
IF i% < j% THEN SWAP array1(i%), array1(j%): GOTO 2 ELSE SWAP array1(i%), array1(h%)
IF i% + i% - g% < h% THEN do.sort array1(), g%, i% - 1: do.sort array1(), i% + 1, h% ELSE do.sort array1(), i% + 1, h%: do.sort array1(), g%, i% - 1
END SUB