' index sort using powerbasic's array sort with tagarray option

'

' the advantage of index sort is that the data being sorted need not

' be moved, but can stay unchanged in their place. this is in contrast

' to ordinary sorting in which the original position of items is lost

' and cannot be retrieved.

'

' an index array is formed which holds the sequence of the data after

' sorting. for example, after ascending sorting the 3rd smallest value

' will be vararray(index(3)). after ordinary sorting it would just have

' been vararray(3).

'

' most of the methods around for sorting data (e.g. quick sort) can

' be modified to perform index sorting. however, if a single array

' needs to be index-sorted, the built-in very fast array sort of

' powerbasic can be applied directly - simply by using the tagarray

' feature as shown in the small code below.

'

' if more complex data structures need to be index-sorted according to

' several prioritised variables, more complex methods will be needed.

' one such method is shown in these links:

'

' http://www.powerbasic.com/support/pb...ad.php?t=22920

'

' http://www.powerbasic.com/support/pb...ad.php?t=23789

'

' erik christensen ------ e.chr@email.dk

------------------

[this message has been edited by erik christensen (edited december 05, 2003).]

'

' the advantage of index sort is that the data being sorted need not

' be moved, but can stay unchanged in their place. this is in contrast

' to ordinary sorting in which the original position of items is lost

' and cannot be retrieved.

'

' an index array is formed which holds the sequence of the data after

' sorting. for example, after ascending sorting the 3rd smallest value

' will be vararray(index(3)). after ordinary sorting it would just have

' been vararray(3).

'

' most of the methods around for sorting data (e.g. quick sort) can

' be modified to perform index sorting. however, if a single array

' needs to be index-sorted, the built-in very fast array sort of

' powerbasic can be applied directly - simply by using the tagarray

' feature as shown in the small code below.

'

' if more complex data structures need to be index-sorted according to

' several prioritised variables, more complex methods will be needed.

' one such method is shown in these links:

'

' http://www.powerbasic.com/support/pb...ad.php?t=22920

'

' http://www.powerbasic.com/support/pb...ad.php?t=23789

'

' erik christensen ------ e.chr@email.dk

Code:

#compile exe #register none #dim all ' sub indexsortusingpbtagarray(byref variable() as long, byref index() as long, _ byval n as long, byval ad as long) register i as long dim varcopy(1:n) as local long ' extra working space for i = 1 to n varcopy(i) = variable(i) index(i) = i next ' ad is an indicator for sorting direction. if ad = 0 then ' zero: ascending sorting (default) array sort varcopy(), tagarray index() elseif ad = 1 then ' one: descending sorting array sort varcopy(), tagarray index(), descend end if erase varcopy end sub ' function pbmain register i as long local n&,t1#,t2#,t$ n=20 dim variable(1:n) as local long, index(1:n) as local long ' t$ = "unsorted numbers:" + $crlf for i=1 to n variable(i) = rnd(1,100) t$ = t$ + str$(variable(i))+" " next t$ = t$ + $crlf + $crlf ' call indexsortusingpbtagarray(variable(), index(), n, 0) ' ascending index sorting ' t$ = t$ + "sorted numbers (ascending order):" + $crlf for i=1 to n t$=t$+str$(variable(index(i)))+" " next t$ = t$ + $crlf + $crlf ' call indexsortusingpbtagarray(variable(), index(), n, 1) ' descending index sorting ' t$ = t$ + "sorted numbers (descending order):" + $crlf for i=1 to n t$=t$+str$(variable(index(i)))+" " next t$ = t$ + $crlf + $crlf + $crlf ' t$ = t$ +"the next is a time test ----- starts on clicking ok" ' msgbox t$,,"index sorting" ' ' time test for index sorting of 100.000 random long integers. n = 100000 redim variable(1:n), index(1:n) ' for i=1 to n variable(i) = rnd(1,5000000) next ' t1 = timer ' start time of sorting ' call indexsortusingpbtagarray(variable(), index(), n, 0) ' ascending index sorting ' t2 = timer ' end time of sorting ' msgbox "sorting time = "+format$(1000 * (t2 - t1), "######")+" ms",,"powerbasic index sort of"+str$(n)+" long integers" ' end function

[this message has been edited by erik christensen (edited december 05, 2003).]

## Comment