[futurebasic] Re: [FB] Sorting differences - FB4 vs FB2

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : November 2008 : Group Archive : Group : All Groups

From: Robert Purves <listrp@...>
Date: Sun, 2 Nov 2008 19:34:10 +1300
Brian Stevens wrote:

> Pierre Zippi wrote:
>
>> OK, I changed "gap = int( gap .3 )" to  "gap = int( gap / 1.3 )" and
>> it still does not exit the DO : UNTIL gap=0 loop. Remember, it worked
>> in FB2.
>
> Just curious: how does the local string array ( named sortedStr$ )  
> return values to the caller? The values in sortedStr$ are sorted  
> based on values in COLUMNLABELS$ ( presumably some global array )  
> but I don't see how the values return to the caller ( maybe we're  
> aren't seeing all the code - or maybe I am? ) . A sort should be  
> generalized: meaning it can sort different arrays. This FN seems to  
> be hard-coded to only use values from specific global variables  
> ( gTotalCol ) and arrays ( COLUMNLABELS ) which restricts the FN's  
> ability to sort other data and forces the programmer to write  
> separate sorts for each unique piece of data. Another clue that  
> maybe the sort is struggling is code like:
>
> size=gTotalCol-2.
> So-called "magic number" corrections are a general indication the  
> code needs an overhaul.


Here's a working comb sort. This implementation is a plain (i.e. not  
indexed) sort.

// Put array(0:n-1) in ascending order.
// Adapted by Robert P. from the FB^3 Release 3 examples
// and further modified by preventing gap values of 9 or 10.
// See http://yagni.com/combsort
local mode
local fn CombSort( array(_maxDim) as SortType, n as long )
'~'1
dim as long gap, switch, j, k

if ( n <= 1 ) then exit fn
n--
gap = n
do
gap = (10*gap)/13
if ( gap == 9 or gap == 10 ) then gap = 11
if ( gap < 1 ) then gap = 1
switch = 0
for j = 0 to n - gap
k = j + gap
long if ( array(j) > array(k) )
swap array(j), array(k)
switch = _zTrue
end if
next
until ( switch == _false and gap == 1 )
end fn

Before calling CombSort() you define SortType appropriate to the array  
that will be passed to it, for example:

#define SortType as Str63
//...set up gData and n...
fn CombSort( gData(0), n )

Robert P.