implémentation tri par sélection + écriture dans les fichiers

git-svn-id: http://cregut.svn.enseeiht.fr/2020/1sn/pim/projets/GH-05@209088 e13453a9-b01f-0410-a051-f404c4f0c485
This commit is contained in:
lfainsin 2020-12-16 16:41:52 +00:00
parent f089f32c83
commit ef9cb81ff8
3 changed files with 132 additions and 47 deletions

View file

@ -108,7 +108,7 @@ package body Google_Naive is
mat(i,j) := 1.0/sum;
end if;
end loop;
else
else -- sum == 0
for j in 0..N-1 loop
mat(i,j) := 1.0 / T_Element(N);
end loop;
@ -138,4 +138,43 @@ package body Google_Naive is
end loop;
end put;
procedure put(file: in out Ada.Text_IO.File_Type; vec: in T_Vecteur) is
begin
for i in 0..N-1 loop
put(file, Float(vec(i)));
new_line(file);
end loop;
end put;
procedure sort_with_index_desc(pi: in T_Vecteur; pi_sorted: out T_Vecteur; pi_index: out T_Vecteur) is
tmp: T_Element;
max: Natural;
begin
pi_sorted := pi;
initialize(pi_index);
for i in 0..N-1 loop
pi_index(i) := T_Element(i);
end loop;
for i in 0..N-2 loop
max := i;
for j in i+1..N-1 loop
if pi_sorted(max) < pi_sorted(j) then
max := j;
end if;
end loop;
if max /= i then
tmp := pi_sorted(i);
pi_sorted(i) := pi_sorted(max);
pi_sorted(max) := tmp;
tmp := pi_index(i);
pi_index(i) := pi_index(max);
pi_index(max) := tmp;
end if;
end loop;
end sort_with_index_desc;
end Google_Naive;

View file

@ -30,6 +30,10 @@ package Google_Naive is
procedure put(vec: in T_Vecteur);
procedure put(mat: in T_Google);
procedure put(file: in out Ada.Text_IO.File_Type; vec: in T_Vecteur);
procedure sort_with_index_desc(pi: in T_Vecteur; pi_sorted: out T_Vecteur; pi_index: out T_Vecteur);
private
type T_Google is array (0..N-1, 0..N-1) of T_Element;

View file

@ -79,55 +79,92 @@ procedure pageRank is
end get_args;
procedure algorithm_naif(N: in Positive;
file: in Ada.Text_IO.File_Type;
alpha: in Float;
ite_max: in Natural) is
procedure choix_type_algo(N: Natural; filename: in Unbounded_String; file: in out Ada.Text_IO.File_Type; alpha: Float; ite_max: Natural; naif: Boolean) is
package Google is
new Google_Naive(T_Element => T_Double, N => N);
use Google;
pi: T_Vecteur;
G: T_Google;
procedure algorithm_naif(N: in Positive;
file: in out Ada.Text_IO.File_Type;
alpha: in Float;
ite_max: in Natural;
pi: out T_Vecteur) is
G: T_Google;
begin
initialize(pi);
put_line("initialized pi");
--put(pi); new_line;
initialize(G);
put_line("initialized G");
put(G); new_line;
create_H(G, file);
put_line("created H");
put(G); new_line;
close(file);
create_S(G);
put_line("created S");
put(G); new_line;
create_G(G, alpha);
put_line("created G");
put(G); new_line;
-- on applique l'algorithme itératif
put("ite: ");
for i in 1..ite_max loop
pi := pi * G;
put(i, 1); put(" ");
end loop; new_line;
new_line;
put_line("final pi:");
put(pi); new_line;
end algorithm_naif;
-- procedure algorithm_creux();
procedure write_to_file(filename: in Unbounded_String; pi_sorted: in T_Vecteur; pi_index: in T_Vecteur) is
file: Ada.Text_IO.File_Type;
begin
create(file, Out_File, To_String(filename & ".p"));
put(file, N);
put(file, alpha);
put(file, ite_max);
new_line(file);
put(file, pi_sorted);
close(file);
create(file, Out_File, To_String(filename & ".ord"));
put(file, pi_index);
close(file);
end write_to_file;
pi, pi_sorted, pi_index: T_Vecteur;
begin
initialize(pi);
put_line("initialized pi");
--put(pi); new_line;
if naif then
algorithm_naif(N, file, alpha, ite_max, pi);
else
--algorithm_creux(N, file, alpha, ite_max, pi);
null;
end if;
initialize(G);
put_line("initialized G");
--put(G); new_line;
create_H(G, file);
put_line("created H");
--put(G); new_line;
create_S(G);
put_line("created S");
--put(G); new_line;
sort_with_index_desc(pi, pi_sorted, pi_index);
create_G(G, alpha);
put_line("created G");
--put(G); new_line;
write_to_file(filename, pi_sorted, pi_index);
-- on applique l'algorithme itératif
put("ite: ");
for i in 1..ite_max loop
pi := pi * G;
put(i, 1); put(" ");
end loop; new_line;
end choix_type_algo;
--new_line;
--put_line("final pi:");
--put(pi); new_line;
end algorithm_naif;
filename: Unbounded_String;
ite_max: Natural := 150;
@ -148,14 +185,19 @@ begin
get(file, N); -- on récupère le nombre de pages
put("N = "); put(N, 1); new_line;
new_line;
choix_type_algo(N, filename, file, alpha, ite_max, naif);
algorithm_naif(N, file, alpha, ite_max);
end pageRank;
new_line;
--sort_with_index(pi, pi_sorted, pi_index);
--write-to_file(filename);
end pageRank;
--def triRapide(L): # Quicksort
-- if len(L) <= 1:
-- return L
-- pvt = L[0]
-- L1, L2 = [], []
-- for i in range(1, len(L)):
-- if L[i] < pvt:
-- L1.append(L[i])
-- else:
-- L2.append(L[i])
-- return triRapide(L1) + [pvt] + triRapide(L2)