-=> On 01-12-98 18:41, Michael Rathburn <=-
-=> spoke to All about sort algorithm <=-
It is almost the dreaded bubble sort. Not too efficient, but
simple.
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> {
The effect of this test is to swap two adjacent elements so that they
are in order. If a swap was done, then the list was not in order to
start with -- and the scan might need to be done again, thus set the
flag test to 0.
MR> temp = table[i];
MR> table[i] = table[ i + 1];
MR> table[i + 1] = temp;
MR> test = 0;
MR> }
This is the end of the I loop. By the end of this loop, the largest
element will have bubbled up to the very end of the array. (a fact
that the routine ignores).
MR> }
This is the end of the while loop. When the program comes out of this,
every thing is in order.
MR> }
Might help to look at what goes on at each stage (at the end of the
loop on the i index variable) for a particular input.
Sample 1: (already sorted)
123456789
123456789 , all in order , test = 1 and done.
sample 2: (was in reverse order -- the worse case for this algorithm)
987654321
876543219 test = 0, do it again.
765432189 test = 0, do it again
....
213456789 test = 0, do it again
123456789 test = 0, do it again
123456789 test = 1, done.
Sample 3: (sort of random looking)
321465978
213456789 test = 0, do it again
123456789 test = 0, do it again
123456789 test = 1, done.
Hope that helped to understand it.
dale (at) min (dot) net
(1:261/1466)
... Shipwrecked on Hesperus in Columbia, Maryland. 19:51:51 14 Jan 98
___ Blue Wave/DOS v2.30
--- Maximus/2 3.01
---------------
* Origin: Owl's Anchor Point (1:261/1466)
|