comp.lang.ada
 help / color / mirror / Atom feed
From: Leo Brewin <leo.brewin@internode.on.net>
Subject: Re: Quick Sort in Rosetta Code
Date: Tue, 9 Feb 2016 23:14:50 -0800 (PST)
Date: 2016-02-09T23:14:50-08:00	[thread overview]
Message-ID: <7a707f07-5a82-430b-becb-5daa4f47844d@googlegroups.com> (raw)
In-Reply-To: <2e3f8f3d-9247-4294-9ee7-961547674bc3@googlegroups.com>

I missed one other change, in the second last if statement the Rosetta code was

   Sort(Item(Item'First..Index_Type'Pred(Right)));

I think that should be

   Sort(Item(Item'First..Index_Type'Pred(Left)));

Here is the complete code

procedure Sort (Item : in out Element_Array) is

   procedure Swap(Left, Right : in out Element_Type) is
      Temp : Element_Type := Left;
   begin
      Left := Right;
      Right := Temp;
   end Swap;

   Pivot_Index : Index_Type;
   Pivot_Value : Element_Type;
   Right       : Index_Type := Item'Last;
   Left        : Index_Type := Item'First;

begin
   if Item'Length > 1 then
      Pivot_Index := Index_Type'Val((Index_Type'Pos(Item'Last) + 1 +
                                     Index_Type'Pos(Item'First)) / 2);
      Pivot_Value := Item(Pivot_Index);

      Left  := Item'First;
      Right := Item'Last;
      loop
         while Left < Item'Last and then Item(Left) < Pivot_Value loop
            Left := Index_Type'Succ(Left);
         end loop;
         while Right > Item'First and then Item(Right) > Pivot_Value loop
            Right := Index_Type'Pred(Right);
         end loop;
         exit when Left >= Right;
         Swap(Item(Left), Item(Right));
         if Left < Item'Last and Right > Item'First then
            Left := Index_Type'Succ(Left);
            Right := Index_Type'Pred(Right);
         end if;
      end loop;
      if Right > Item'First then
         Sort(Item(Item'First..Index_Type'Pred(Left)));
      end if;
      if Left < Item'Last then
         Sort(Item(Left..Item'Last));
      end if;
   end if;
end Sort;


  parent reply	other threads:[~2016-02-10  7:14 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-10  5:27 Quick Sort in Rosetta Code gautier_niouzes
2016-02-10  5:43 ` Jeffrey R. Carter
2016-02-10 13:50   ` robin.vowels
2016-02-10 21:39     ` Jeffrey R. Carter
2016-02-10  7:00 ` Leo Brewin
2016-02-10  7:14 ` Leo Brewin [this message]
2016-02-10 10:04   ` gautier_niouzes
2016-02-10 10:16   ` gautier_niouzes
2016-02-10 20:41 ` Simon Wright
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox