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.Text_IO; use Ada.Text_IO;
-- with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Vector; with Vector;
procedure pageRank is procedure test_tri is
Type T_Double is digits 3; Type T_Double is digits 3;
N: Natural := 10; N: constant Natural := 10;
package Vector_Double is package Vector_Double is
new Vector(T_Element => T_Double, new Vector(T_Element => T_Double,
N => N); N => N);
use Vector_Double; use Vector_Double;
vec: T_Vecteur_Element; vec: T_Vecteur_Element;
vec_index: T_Vecteur_Natural;
begin begin
@ -29,11 +29,15 @@ begin
vec(8) := 6.0; vec(8) := 6.0;
vec(9) := 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 loop;
end sort_insert_desc; end sort_insert_desc;
procedure quicksort(vec: in out T_Vecteur_Element; vec_index: in out T_Vecteur_Natural; a, b: in Natural) is procedure quicksort(vec: in out T_Vecteur_Element; low, high: Natural) is
pivot: Natural := a;
i: Natural := a; procedure swap(left, right: Natural) is
tmp: T_Element; tmp : constant T_Element := vec(left);
begin
vec(left) := vec(right);
vec(right) := tmp;
end swap;
begin begin
if b - a > 1 then if high - low > 0 then
for j in a+1..b loop declare
if vec(j) <= vec(pivot) then pivot : constant T_Element := vec(low);
i := i + 1; right : Natural := high;
tmp := vec(i); left : Natural := low;
vec(i) := vec(j); begin
vec(j) := tmp; loop
else while left < right and not(pivot < vec(left)) loop
null; 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 if;
end loop;
tmp := vec(i); --put(left); put(right); put(low); put(high); new_line;
vec(i) := vec(pivot); quicksort(vec, low, right);
vec(pivot) := tmp; quicksort(vec, left, high);
end;
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;
end if; end if;
end quicksort; 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 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 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; end Vector;