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:
parent
07984f97df
commit
ef72ba257c
|
@ -1,12 +1,13 @@
|
||||||
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,
|
||||||
|
@ -14,7 +15,6 @@ procedure pageRank is
|
||||||
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;
|
||||||
|
|
|
@ -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
|
begin
|
||||||
if b - a > 1 then
|
vec(left) := vec(right);
|
||||||
for j in a+1..b loop
|
vec(right) := tmp;
|
||||||
if vec(j) <= vec(pivot) then
|
end swap;
|
||||||
i := i + 1;
|
|
||||||
tmp := vec(i);
|
begin
|
||||||
vec(i) := vec(j);
|
if high - low > 0 then
|
||||||
vec(j) := tmp;
|
declare
|
||||||
else
|
pivot : constant T_Element := vec(low);
|
||||||
null;
|
right : Natural := high;
|
||||||
end if;
|
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;
|
end loop;
|
||||||
|
|
||||||
tmp := vec(i);
|
exit when right <= left;
|
||||||
vec(i) := vec(pivot);
|
swap(left, right);
|
||||||
vec(pivot) := tmp;
|
left := left + 1;
|
||||||
|
right := right - 1;
|
||||||
|
end loop;
|
||||||
|
|
||||||
pivot := i;
|
if right = high then
|
||||||
|
right := right - 1;
|
||||||
put(vec); new_line;
|
swap(low, high);
|
||||||
|
|
||||||
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;
|
end if;
|
||||||
else
|
|
||||||
null;
|
if left = low then
|
||||||
|
left := left - 1;
|
||||||
|
end if;
|
||||||
|
|
||||||
|
--put(left); put(right); put(low); put(high); new_line;
|
||||||
|
quicksort(vec, low, right);
|
||||||
|
quicksort(vec, left, high);
|
||||||
|
end;
|
||||||
end if;
|
end if;
|
||||||
end quicksort;
|
end quicksort;
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
Loading…
Reference in a new issue