TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: MICHAEL RATHBURN
from: KURT KUZBA
date: 1998-01-16 01:05:00
subject: sort algorithm

MR>         //sort
MR>         int temp, test = 0;
MR>         // this is the confusing part :-
MR>         while(test == 0)
MR>         {
MR>                 test = 1;
MR>                 for (i = 0; i < (MAX - 1); i++)
MR>                 {
MR>                         if (table[i] < table[i + 1])
MR>                         {
MR>                                 temp = table[i];
MR>                                 table[i] = table[ i + 1];
MR>                                 table[i + 1] = temp;
MR>                                 test = 0;
MR>                         }
MR>                 }
MR>         }
   It's pretty simple, really...
int temp, test = 0;     //  define a test condition
while(test == 0)        //  while a test condition exists
{
   test = 1;            //  clear the test condition
   for (i = 0; i < (MAX - 1); i++)
   {                    //  for each element in the array
      if (table[i] < table[i + 1])
      {                 //  if the present element is less than
                        //  the next element
         temp = table[i];
                        //  save the value of this element
         table[i] = table[ i + 1];
                        //  move the next element to this slot
         table[i + 1] = temp;
                        //  make present element next element
         test = 0;
                        //  we still hit a swap, so the sort
                        //  isn't done. reset test condition
      }
   }
}
   And that's all there is to it. When all the elements are in
   their proper order, the test condition will not be reset to
   0, and the while() loop will terminate.
   This might make it more readable:
int swap, sorting, next, present;
do {
   sorting = 0;
   for(present = 0; present < (MAX - 1); present++)
      if(table[present] < table[next = (present + 1)])
      {
         swap = table[present];
         table[present] = table[next];
         table[next] = swap;
         sorting = 1;
      }
}  while(sorting);
   Using a slightly more wordy presentation, or copious
   commentary, can help you when you try to read someone
   else's code, or try to read your own after not seeing
   it for an extended period. What is perfectly clear at
   the time may be obfuscated in retrospect.
   Running the for(;;) loop from MAX to 1 will sort them
   in reverse order, assuming next - (present - 1), as will
   reversing the conditional in the if().
   I actually favor running it backwards, myself...
int swap, sorting, next, present;
do {
   sorting = 0;
   for(present = MAX - 1; present > 1; present--)
      if(table[present] > table[next = (present - 1)])
      {
         swap = table[present];
         table[present] = table[next];
         table[next] = swap;
         sorting = 1;
      }
}  while(sorting);
   A minor point, removing a math operation from the for(;;).
---
> ] Remain calm and think. I will help you. - Kwai Chang CPUfan.
---
---------------
* Origin: *YOPS ]I[* 3.1 GIG * RA/FD/FE RADist * Milwaukee, WI (1:154/750)

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™.