From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.66.150.131 with SMTP id ui3mr17671820pab.42.1455088491375; Tue, 09 Feb 2016 23:14:51 -0800 (PST) X-Received: by 10.182.101.196 with SMTP id fi4mr578703obb.4.1455088491339; Tue, 09 Feb 2016 23:14:51 -0800 (PST) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!news.glorb.com!hb3no2896567igb.0!news-out.google.com!l1ni8543igd.0!nntp.google.com!ok5no341177igc.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Tue, 9 Feb 2016 23:14:50 -0800 (PST) In-Reply-To: <2e3f8f3d-9247-4294-9ee7-961547674bc3@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=118.209.28.175; posting-account=l8GBMwoAAADCbqdOJSbg4dBRqkD14dJd NNTP-Posting-Host: 118.209.28.175 References: <2e3f8f3d-9247-4294-9ee7-961547674bc3@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <7a707f07-5a82-430b-becb-5daa4f47844d@googlegroups.com> Subject: Re: Quick Sort in Rosetta Code From: Leo Brewin Injection-Date: Wed, 10 Feb 2016 07:14:51 +0000 Content-Type: text/plain; charset=ISO-8859-1 Xref: news.eternal-september.org comp.lang.ada:29474 Date: 2016-02-09T23:14:50-08:00 List-Id: 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;