Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sorting alphabeticly
#11
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....

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]
Reply
#12
Traditionally, the Bubble Sort has been implemented with only one FOR LOOP, including, as shiftlynx said, a flag or switch to indicate if the last iteration of the FOR LOOP caused a SWAP operation.

EDITED: At first glance, the use of two FOR LOOPS looked like a Shell Sort to me, but I was wrong. To be perfectly honest, when I don't actually understand a sort algorithm having multiple loops, I tend to confuse the type of algorithm (Selection, Insertion, Shell, Radix, etc.)

Incidently, the original Bubble Sort caused the smallest element to "bubble" to the top of the list. A modification of this, the Sift Sort, causes the largest element to "sift" down to the bottom of the list. If you look at most implementations of the Bubble Sort, you will notice that they are actually a Sift Sort.
*****
Reply
#13
shiftLynx: That's pretty cool, I should have thought of that :wink:
COUNT HACKED BY RAZVEEE

RAZVEE IS A SCRIPT KIDDIE- hacker9
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)