quicksort fonctionne

git-svn-id: http://cregut.svn.enseeiht.fr/2020/1sn/pim/projets/GH-05@210350 e13453a9-b01f-0410-a051-f404c4f0c485
This commit is contained in:
lfainsin 2020-12-23 15:16:37 +00:00
parent 07984f97df
commit ef72ba257c
3 changed files with 57 additions and 43 deletions

View file

@ -1,20 +1,20 @@
with Ada.Text_IO; use Ada.Text_IO;
-- with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Vector;
procedure pageRank is
procedure test_tri is
Type T_Double is digits 3;
N: Natural := 10;
N: constant Natural := 10;
package Vector_Double is
new Vector(T_Element => T_Double,
N => N);
N => N);
use Vector_Double;
vec: T_Vecteur_Element;
vec_index: T_Vecteur_Natural;
begin
@ -29,11 +29,15 @@ begin
vec(8) := 6.0;
vec(9) := 6.0;
put_line("avant:");
put(vec);
new_line;
quicksort(vec, vec_index, 0, N-1);
put_line("après:");
put(vec);
end pageRank;
new_line;
put_line("avant:");
put(vec); new_line;
quicksort(vec, 0, N-1);
put_line("après:");
put(vec); new_line;
end test_tri;

View file

@ -74,40 +74,49 @@ package body vector is
end loop;
end sort_insert_desc;
procedure quicksort(vec: in out T_Vecteur_Element; vec_index: in out T_Vecteur_Natural; a, b: in Natural) is
pivot: Natural := a;
i: Natural := a;
tmp: T_Element;
procedure quicksort(vec: in out T_Vecteur_Element; low, high: Natural) is
procedure swap(left, right: Natural) is
tmp : constant T_Element := vec(left);
begin
vec(left) := vec(right);
vec(right) := tmp;
end swap;
begin
if b - a > 1 then
for j in a+1..b loop
if vec(j) <= vec(pivot) then
i := i + 1;
tmp := vec(i);
vec(i) := vec(j);
vec(j) := tmp;
else
null;
if high - low > 0 then
declare
pivot : constant T_Element := vec(low);
right : Natural := high;
left : Natural := low;
begin
loop
while left < right and not(pivot < vec(left)) loop
left := left + 1;
end loop;
while pivot < vec(right) loop
right := right - 1;
end loop;
exit when right <= left;
swap(left, right);
left := left + 1;
right := right - 1;
end loop;
if right = high then
right := right - 1;
swap(low, high);
end if;
if left = low then
left := left - 1;
end if;
end loop;
tmp := vec(i);
vec(i) := vec(pivot);
vec(pivot) := tmp;
pivot := i;
put(vec); new_line;
if pivot - 1 > 0 then
quicksort(vec, vec_index, a, pivot-1);
elsif b - pivot - 1 > 0 then
quicksort(vec, vec_index, pivot+1, b);
else
null;
end if;
else
null;
--put(left); put(right); put(low); put(high); new_line;
quicksort(vec, low, right);
quicksort(vec, left, high);
end;
end if;
end quicksort;

View file

@ -25,6 +25,7 @@ package Vector is
procedure put(file: in out Ada.Text_IO.File_Type; vec: in T_Vecteur_Natural);
procedure sort_insert_desc(vec: in out T_Vecteur_Element; vec_index: in out T_Vecteur_Natural);
procedure quicksort(vec: in out T_Vecteur_Element; vec_index: in out T_Vecteur_Natural; a, b: in Natural);
procedure quicksort(vec: in out T_Vecteur_Element; low, high: Natural);
end Vector;