TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: KURT KUZBA
from: DAVID NOON
date: 1998-04-14 20:39:00
subject: Bubble And Squeak Sortin

In a message dated 04-12-98, Kurt Kuzba said to Carey Bloodworth about
Bubble And Squeak Sorting
Hi Kurt,
KK>   Now you have only one swap for each element which is out of
KK>   place. This is not really a bubble sort, though, since the
KK>   elements don't actually migrate toward their place, but just
KK>   jump there all at once. I guess it would have to be a sort
KK>   of a hyper-bubble sort. 
Actually, it's a selection sort. That's a whole other algorithm.
KK>/*   bubble.c   PUBLIC DOMAIN   */
KK>#include 
KK>#include 
KK>void bubblesort(int *array, int elements)
KK>{
KK>   int outer, inner, swap, tmp, end, reverse;
KK>   reverse = (elements < 0) ? 1 : 0;
KK>   elements = reverse ? -elements : elements;
KK>   end = elements - 1;
KK>/*
KK>   We do this so that we need not shake our fists
KK>   toward Borland International in our rage.   :)
KK>*/
Secretly we all love those guys in Scott's Valley.
KK>   for(outer = 0; outer < end; outer++)
KK>   {
KK>     swap = outer;
KK>     for(inner = outer; inner < elements; inner++)
KK>       if((array[swap] > array[inner] && !reverse)
KK>         || (array[swap] < array[inner] && reverse))
Yick! Choosing your sort direction for each and every iteration!!
If you must do this, why not use the following?
    if (reverse ? array[swap]  array[inner])
It is still dog slow, but it should produce better code than what you
already have. [Especially using Borland's wonderful optimizer.]
KK>            swap = inner;
KK>     if(swap > outer)
KK>     {
KK>       tmp = array[outer];
KK>       array[outer] = array[swap];
KK>       array[swap] = tmp;
KK>     }
KK>   }
KK>}
Yes, definitely NOT a bubble sort.
So, how about changing the name of the subroutine from bubblesort() to
selectionsort()?
Regards
Dave

___
 * MR/2 2.25 #353 * If your VCR still blinks 12:00, don't try Linux.
--- Maximus/2 3.01
---------------
* Origin: DoNoR/2,Woking UK (44-1483-717905) (2:440/4)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.