06-02-2005, 10:29 PM
There is one small modification that can be made to a bubble sort to make it faster in certain cases; that's to keep a flag to record whether you did any swapping in the pass you're doing at the moment. If you didn't swap any values then the list must be in order. It could be that the first pass completely sorts the list if the names are only a little bit out of order, so in that case it's quite handy.
Here's the modified sort code....
I've written a couple of tutorials on a few simple sorting algorithms... they're on my site: http://www.cdsoft.co.uk/tutorials.php
Here's the modified sort code....
Code:
' sort
for i = 0 to count-2
swapped = 0
for j = i+1 to count-1
if names(i) > names(j) then
swap names(i), names(j)
swapped = 1
end if
next j
' if the list has been sorted already, stop processing it.
if swapped = 0 then exit for
next i
I've written a couple of tutorials on a few simple sorting algorithms... they're on my site: http://www.cdsoft.co.uk/tutorials.php
img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img]