projet-programmation-impera.../src/vector.adb

124 lines
3.4 KiB
Ada
Raw Normal View History

with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
package body vector is
procedure initialize(vec: in out T_Vecteur_Element; value: in T_Element) is
begin
for i in 0..N-1 loop
vec(i) := value;
end loop;
end initialize;
procedure initialize(vec: in out T_Vecteur_Natural) is
begin
for i in 0..N-1 loop
vec(i) := i;
end loop;
end initialize;
function sum(vec: in T_Vecteur_Element) return T_Element is
s: T_Element := 0.0;
begin
for i in 0..N-1 loop
s := s + vec(i);
end loop;
return s;
end sum;
procedure put(vec: in T_Vecteur_Element) is
use Text_T_Element;
begin
for i in 0..N-1 loop
put(vec(i)); new_line;
end loop;
end put;
procedure put(file: in out Ada.Text_IO.File_Type; vec: in T_Vecteur_Element) is
use Text_T_Element;
begin
for i in 0..N-1 loop
put(file, vec(i), Fore=>1, Aft=>10);
new_line(file);
end loop;
end put;
procedure put(file: in out Ada.Text_IO.File_Type; vec: in T_Vecteur_Natural) is
begin
for i in 0..N-1 loop
put(file, vec(i), 1);
new_line(file);
end loop;
end put;
procedure sort_insert_desc(vec: in out T_Vecteur_Element; vec_index: in out T_Vecteur_Natural) is
tmp_Element: T_Element;
tmp_Natural: Natural;
max: Natural;
begin
for i in 0..N-2 loop
max := i;
for j in i+1..N-1 loop
if vec(max) <= vec(j) then
max := j;
end if;
end loop;
if max /= i then
tmp_Element := vec(i);
vec(i) := vec(max);
vec(max) := tmp_Element;
tmp_Natural := vec_index(i);
vec_index(i) := vec_index(max);
vec_index(max) := tmp_Natural;
end if;
end loop;
end sort_insert_desc;
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 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;
--put(left); put(right); put(low); put(high); new_line;
quicksort(vec, low, right);
quicksort(vec, left, high);
end;
end if;
end quicksort;
end vector;