From: Jeff Carter <jrcarter@acm.org>
Subject: Re: No Go To's Forever!
Date: 2000/03/23
Date: 2000-03-23T00:00:00+00:00 [thread overview]
Message-ID: <38DA9505.F1AE2989@acm.org> (raw)
In-Reply-To: 8bdbof$t19$1@nnrp1.deja.com
Robert Dewar wrote:
> Let's once again look at my original:
>
> > <<Do_It_Again>>
> > for J in 1 .. N - 1 loop
> > if D (J) < D (J + 1) then
> > Swap (D (J), D (J + 1));
> > goto Do_It_Again;
> > end if;
> > end loop;
> Continue := True;
> Outer : while Continue loop
> Continue := False;
> Inner : for J in 1 .. N - 1 loop
> if D (J) < D (J + 1) then
> Swap (D (J), D (J + 1));
> Continue := True;
> exit Inner;
> end if;
> end loop Inner;
> end loop Outer;
Personally, I find both of these a bit difficult to follow due to the
lack of documentation. Maybe I'm wrong, but I think a variation on the
goto-less version is easier to document because the 2 loop are explicit:
-- Sort D into descending order
Repeat_Until_Sorted : loop
Sorted := True; -- Assume D is sorted
Find_Out_Of_Order_Pair : for J in D'First .. D'Last - 1 loop
if D (J) < D (J + 1) then -- Found an out-of-order pair if True
Swap (D (J), D (J + 1) );
Sorted := False; -- We have not determined that D is sorted
exit Find_Out_Of_Order_Pair; -- Repeat to see if D now sorted
end if;
end loop Find_Out_Of_Order_Pair;
exit Repeat_Until_Sorted when Sorted;
-- Did not find out-of-order pair if Sorted
end loop Repeat_Until_Sorted;
Now I can see what the algorithm is doing: It scans D looking for an
out-of-order pair of adjacent values. If it doesn't find one, then D is
sorted. If it does find one, it swaps them and repeats the entire
process. This looks like elimination of tail-recursion:
Find_Out_Of_Order_Pair : for J in D'First .. D'Last - 1 loop
if D (J) < D (J + 1) then
Swap (D (J), D (J + 1) );
Sort (D);
exit Find_Out_Of_Order_Pair;
end if;
end loop Find_Out_Of_Order_Pair;
--
Jeff Carter
"Now go away or I shall taunt you a second time."
Monty Python & the Holy Grail
next prev parent reply other threads:[~2000-03-23 0:00 UTC|newest]
Thread overview: 105+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-03-21 0:00 No Go To's Forever! Bill Dale
2000-03-21 0:00 ` David Starner
2000-03-21 0:00 ` Bill Dale
2000-03-22 0:00 ` Robert Dewar
2000-03-22 0:00 ` Robert Dewar
2000-03-22 0:00 ` Robert Dewar
2000-03-22 0:00 ` Robert A Duff
2000-03-22 0:00 ` Roger Barnett
2000-03-22 0:00 ` Charles Hixson
2000-03-22 0:00 ` Paul Graham
2000-03-22 0:00 ` Robert Dewar
2000-03-22 0:00 ` Paul Graham
2000-03-23 0:00 ` Robert Dewar
2000-03-23 0:00 ` Ted Dennison
2000-03-23 0:00 ` Robert Dewar
2000-03-23 0:00 ` Paul Graham
2000-03-23 0:00 ` Robert Dewar
2000-03-23 0:00 ` Larry Kilgallen
2000-03-22 0:00 ` Michael P. Walsh
2000-03-22 0:00 ` Charles Hixson
2000-04-06 0:00 ` Wes Groleau
2000-04-07 0:00 ` Charles Hixson
2000-03-22 0:00 ` Brian Rogoff
2000-03-22 0:00 ` Ted Dennison
2000-03-22 0:00 ` Michael P. Walsh
2000-03-23 0:00 ` Robert Dewar
2000-03-21 0:00 ` Charles Hixson
2000-03-21 0:00 ` Gautier
2000-03-21 0:00 ` Robert Dewar
2000-03-21 0:00 ` Michael P. Walsh
2000-03-21 0:00 ` Andrew Berg
2000-03-22 0:00 ` Wes Groleau
2000-03-22 0:00 ` No Go To's Forever! (I'm sorry I spoke...) dis90072
2000-03-23 0:00 ` tmoran
2000-03-23 0:00 ` Larry Kilgallen
2000-03-22 0:00 ` No Go To's Forever! Ken Garlington
2000-03-22 0:00 ` Marin D. Condic
2000-03-22 0:00 ` Roger Barnett
2000-03-22 0:00 ` Larry Kilgallen
2000-03-23 0:00 ` Robert Dewar
2000-03-23 0:00 ` Keith Thompson
2000-03-24 0:00 ` Marin D. Condic
2000-03-24 0:00 ` Ted Dennison
2000-03-27 0:00 ` Keith Thompson
2000-03-28 0:00 ` Come From Forever! (was: No Go To's Forever!) Ted Dennison
2000-03-29 0:00 ` Keith Thompson
2000-03-25 0:00 ` No Go To's Forever! Richard D Riehle
2000-03-22 0:00 ` Tim Gahnström
2000-03-21 0:00 ` David Starner
2000-03-22 0:00 ` Robert Dewar
2000-03-22 0:00 ` Ken Garlington
2000-03-21 0:00 ` Keith Thompson
2000-03-22 0:00 ` Robert Dewar
2000-03-23 0:00 ` Ken Garlington
2000-03-22 0:00 ` Robert Dewar
2000-03-23 0:00 ` Tim Gahnstr�m
2000-03-22 0:00 ` tmoran
2000-03-22 0:00 ` Robert Dewar
2000-03-22 0:00 ` tmoran
2000-03-23 0:00 ` Robert Dewar
2000-03-23 0:00 ` tmoran
2000-03-23 0:00 ` Robert Dewar
2000-03-23 0:00 ` tmoran
2000-03-24 0:00 ` Robert Dewar
2000-03-24 0:00 ` Robert Dewar
2000-04-16 0:00 ` Kenneth Almquist
2000-04-17 0:00 ` Robert Dewar
2000-04-17 0:00 ` Robert Dewar
2000-04-18 0:00 ` Dale Stanbrough
2000-04-18 0:00 ` David Starner
2000-04-18 0:00 ` Robert Dewar
2000-03-23 0:00 ` Jeff Carter [this message]
2000-03-24 0:00 ` Robert Dewar
2000-03-29 0:00 ` Martin Dowie
2000-03-29 0:00 ` Florian Weimer
2000-03-29 0:00 ` Robert Dewar
2000-03-30 0:00 ` Robert A Duff
2000-04-01 0:00 ` Robert Dewar
2000-04-01 0:00 ` Robert A Duff
2000-04-02 0:00 ` Robert Dewar
2000-04-21 0:00 ` Florian Weimer
2000-04-21 0:00 ` Robert Dewar
2000-03-29 0:00 ` Robert Dewar
2000-03-29 0:00 ` Robert Dewar
2000-03-24 0:00 ` tmoran
2000-03-22 0:00 ` Richard D Riehle
2000-03-23 0:00 ` Jeff Carter
2000-03-23 0:00 ` Michael P. Walsh
2000-03-23 0:00 ` Brian Rogoff
2000-03-23 0:00 ` Robert Dewar
2000-03-22 0:00 ` Pascal Obry
2000-03-22 0:00 ` Marin D. Condic
2000-03-22 0:00 ` Robert Dewar
2000-03-22 0:00 ` Jon S Anthony
2000-03-22 0:00 ` Roger Barnett
2000-03-23 0:00 ` Robert Dewar
2000-03-23 0:00 ` Roger Barnett
2000-03-24 0:00 ` Robert Dewar
2000-03-23 0:00 ` Robert Dewar
2000-03-22 0:00 ` Jon S Anthony
2000-03-22 0:00 ` Robert Dewar
2000-03-22 0:00 ` Robert Dewar
2000-03-23 0:00 ` Chris Morgan
[not found] ` <01bf9436$9c054880$2c5101be@bthomas4663>
2000-03-23 0:00 ` Robert Dewar
2000-03-23 0:00 ` Ken Garlington
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox