Bubble Sort In Basic - Sorting an indexed array
nick bernard
Posts: 329
Hail Board,
i'm developing a little project that requires sorting and i just wanted to share what i have developed and exchange ideas about the algorithm and code in general. the tricky thing about my bubble sort is the way i indexed data in the scratch pad.
questions, comments, advise?
here is the code:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
" Hey! Why is there silicone on my hemostats?"
i'm developing a little project that requires sorting and i just wanted to share what i have developed and exchange ideas about the algorithm and code in general. the tricky thing about my bubble sort is the way i indexed data in the scratch pad.
questions, comments, advise?
here is the code:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
" Hey! Why is there silicone on my hemostats?"
txt
1K
Comments
·· Nice job!· And no sorting in the EEPROM!· LOL· I will have to check this out in more detail later, but I think it's the first sorting routine I have seen on the BASIC Stamp.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
there arent many applications for a bubble sort in the world of stamping. i suppose that's why it never came up. In any case i always liked the bubble sort because its a simple, easy-to-follow teaching tool for sorting even if its inefficient.
rox on
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
" Hey! Why is there silicone on my hemostats?"
One suggestion, is that in some cases the the BUBBLE subroutine might be called when the array is already sorted, or when only one score has been added since the previous sort. In that case it there is no point in passing through the loop more than once or twice. I'd add a one bit flag variable that is set to TRUE before executing the inner loop, and set equal to FALSE if any swap takes place while passing through the inner loop. After the inner loop a test, IF noswaps THEN EXIT. Another small thing, if NOP is a constant, then NOP-2 is also a constant and a little program space and speed can be saved by also defining NOP2 CON NOP-2.
I have a median routine at www.emesystems.com/BS2math5.htm, but I think your ranked bubble sort would be a better way to go about that. I came at it in a different way, iteratively extracting the minimum and maximum from a list.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
do while (noswaps)
nexted swaping routine
loop
this would minimize the number of iterations of the swapping routine
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
" Hey! Why is there silicone on my hemostats?"