From: gautier_niouzes@hotmail.com
Subject: Re: Quick Sort in Rosetta Code
Date: Wed, 10 Feb 2016 02:16:41 -0800 (PST)
Date: 2016-02-10T02:16:41-08:00 [thread overview]
Message-ID: <678d43f6-59fc-4768-b86c-9f5f43111b7b@googlegroups.com> (raw)
In-Reply-To: <7a707f07-5a82-430b-becb-5daa4f47844d@googlegroups.com>
I did not dig too far into the fixing (the tricky thing is to avoid going out of range with indices) but here is, as an inspiration, a simplified case where the index type is integer, and this one is working (I've taken the C version to start with):
with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;
--with Ada.Text_IO; use Ada.Text_IO;
procedure Qsort is
type Item_array is array(Integer Range <>) of Float;
procedure Quick_sort (a: in out Item_array) is
n: Integer:= a'Length;
i, j: Integer;
p, t: Float;
begin
if n < 2 then
return;
end if;
p := a(n / 2 + a'First);
i:= 0;
j:= n - 1;
loop
while a(i + a'First) < p loop
i:= i + 1;
end loop;
while p < a(j + a'First) loop
j:= j - 1;
end loop;
exit when i >= j;
t := a(i + a'First);
a(i + a'First) := a(j + a'First);
a(j + a'First) := t;
i:= i + 1;
j:= j - 1;
end loop;
Quick_sort(a(a'First .. a'First + i - 1));
Quick_sort(a(a'First + i .. a'Last));
end;
gen: Generator;
begin
Reset(gen);
for n in 1..100 loop
for round in 1..500 loop
declare
start: Integer:= Integer(10.0 * (Random(gen)-0.5));
a: Item_array(start .. start + n - 1);
begin
for i in a'Range loop
a(i):= Random(gen);
end loop;
Quick_sort(a);
for i in a'Range loop
-- Put_Line(Float'Image(a(i)));
if i > a'First and then a(i-1) > a(i) then
raise Program_Error; -- Buggy sorting
end if;
end loop;
end;
end loop;
end loop;
end;
_________________________
Gautier's Ada programming
http://sf.net/users/gdemont/
next prev parent reply other threads:[~2016-02-10 10:16 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
2016-02-10 10:04 ` gautier_niouzes
2016-02-10 10:16 ` gautier_niouzes [this message]
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