2020-12-21 20:17:50 +00:00
|
|
|
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
|
2020-12-19 15:12:22 +00:00
|
|
|
|
|
|
|
package body vector is
|
|
|
|
|
2020-12-22 13:26:25 +00:00
|
|
|
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
|
2020-12-19 15:12:22 +00:00
|
|
|
begin
|
|
|
|
for i in 0..N-1 loop
|
|
|
|
vec(i) := i;
|
|
|
|
end loop;
|
|
|
|
end initialize;
|
|
|
|
|
2020-12-22 13:26:25 +00:00
|
|
|
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
|
2020-12-23 13:43:03 +00:00
|
|
|
use Text_T_Element;
|
2020-12-19 15:12:22 +00:00
|
|
|
begin
|
|
|
|
for i in 0..N-1 loop
|
|
|
|
put(vec(i)); new_line;
|
|
|
|
end loop;
|
|
|
|
end put;
|
|
|
|
|
2020-12-22 13:26:25 +00:00
|
|
|
procedure put(file: in out Ada.Text_IO.File_Type; vec: in T_Vecteur_Element) is
|
2020-12-23 13:43:03 +00:00
|
|
|
use Text_T_Element;
|
2020-12-19 15:12:22 +00:00
|
|
|
begin
|
|
|
|
for i in 0..N-1 loop
|
2020-12-22 13:26:25 +00:00
|
|
|
put(file, vec(i), Fore=>1, Aft=>10);
|
2020-12-19 15:12:22 +00:00
|
|
|
new_line(file);
|
|
|
|
end loop;
|
|
|
|
end put;
|
|
|
|
|
2020-12-22 13:26:25 +00:00
|
|
|
procedure put(file: in out Ada.Text_IO.File_Type; vec: in T_Vecteur_Natural) is
|
2020-12-19 15:12:22 +00:00
|
|
|
begin
|
|
|
|
for i in 0..N-1 loop
|
|
|
|
put(file, vec(i), 1);
|
|
|
|
new_line(file);
|
|
|
|
end loop;
|
|
|
|
end put;
|
|
|
|
|
2020-12-22 13:26:25 +00:00
|
|
|
procedure sort_with_index_desc(vec: in out T_Vecteur_Element; vec_index: in out T_Vecteur_Natural) is
|
2020-12-19 15:12:22 +00:00
|
|
|
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
|
2020-12-22 13:26:25 +00:00
|
|
|
if vec(max) <= vec(j) then
|
2020-12-19 15:12:22 +00:00
|
|
|
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_with_index_desc;
|
|
|
|
|
2020-12-23 13:43:03 +00:00
|
|
|
<<<<<<< .mine
|
|
|
|
||||||| .r210321
|
|
|
|
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;
|
|
|
|
begin
|
|
|
|
if b - a > 1 then
|
|
|
|
for j in a..b loop
|
|
|
|
if vec(j) <= vec(pivot) then
|
|
|
|
i := i + 1;
|
|
|
|
tmp := vec(i);
|
|
|
|
vec(i) := vec(j);
|
|
|
|
vec(j) := tmp;
|
|
|
|
else
|
|
|
|
null;
|
|
|
|
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;
|
|
|
|
end if;
|
|
|
|
end quicksort;
|
|
|
|
|
|
|
|
=======
|
2020-12-22 15:55:58 +00:00
|
|
|
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;
|
|
|
|
begin
|
|
|
|
if b - a > 1 then
|
2020-12-22 19:03:03 +00:00
|
|
|
for j in a+1..b loop
|
2020-12-22 15:55:58 +00:00
|
|
|
if vec(j) <= vec(pivot) then
|
|
|
|
i := i + 1;
|
|
|
|
tmp := vec(i);
|
|
|
|
vec(i) := vec(j);
|
|
|
|
vec(j) := tmp;
|
|
|
|
else
|
|
|
|
null;
|
|
|
|
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;
|
|
|
|
end if;
|
|
|
|
end quicksort;
|
|
|
|
|
2020-12-23 13:43:03 +00:00
|
|
|
>>>>>>> .r210345
|
2020-12-19 15:12:22 +00:00
|
|
|
end vector;
|
2020-12-23 13:43:03 +00:00
|
|
|
<<<<<<< .mine
|
|
|
|
||||||| .r210321
|
|
|
|
|
|
|
|
-- 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)
|
|
|
|
|
|
|
|
-- procedure Quick_Sort (A : in out Element_Array) is
|
|
|
|
|
|
|
|
-- procedure Swap(Left, Right : Index) is
|
|
|
|
-- Temp : Element := A (Left);
|
|
|
|
-- begin
|
|
|
|
-- A (Left) := A (Right);
|
|
|
|
-- A (Right) := Temp;
|
|
|
|
-- end Swap;
|
|
|
|
|
|
|
|
-- begin
|
|
|
|
-- if A'Length > 1 then
|
|
|
|
-- declare
|
|
|
|
-- Pivot_Value : Element := A (A'First);
|
|
|
|
-- Right : Index := A'Last;
|
|
|
|
-- Left : Index := A'First;
|
|
|
|
-- begin
|
|
|
|
-- loop
|
|
|
|
-- while Left < Right and not (Pivot_Value < A (Left)) loop
|
|
|
|
-- Left := Index'Succ (Left);
|
|
|
|
-- end loop;
|
|
|
|
-- while Pivot_Value < A (Right) loop
|
|
|
|
-- Right := Index'Pred (Right);
|
|
|
|
-- end loop;
|
|
|
|
-- exit when Right <= Left;
|
|
|
|
-- Swap (Left, Right);
|
|
|
|
-- Left := Index'Succ (Left);
|
|
|
|
-- Right := Index'Pred (Right);
|
|
|
|
-- end loop;
|
|
|
|
-- if Right = A'Last then
|
|
|
|
-- Right := Index'Pred (Right);
|
|
|
|
-- Swap (A'First, A'Last);
|
|
|
|
-- end if;
|
|
|
|
-- if Left = A'First then
|
|
|
|
-- Left := Index'Succ (Left);
|
|
|
|
-- end if;
|
|
|
|
-- Quick_Sort (A (A'First .. Right));
|
|
|
|
-- Quick_Sort (A (Left .. A'Last));
|
|
|
|
-- end;
|
|
|
|
-- end if;
|
|
|
|
-- end Quick_Sort;=======
|
2020-12-22 15:55:58 +00:00
|
|
|
|
|
|
|
-- 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)
|
|
|
|
|
|
|
|
-- procedure Quick_Sort (A : in out Element_Array) is
|
|
|
|
|
|
|
|
-- procedure Swap(Left, Right : Index) is
|
|
|
|
-- Temp : Element := A (Left);
|
|
|
|
-- begin
|
|
|
|
-- A (Left) := A (Right);
|
|
|
|
-- A (Right) := Temp;
|
|
|
|
-- end Swap;
|
|
|
|
|
|
|
|
-- begin
|
|
|
|
-- if A'Length > 1 then
|
|
|
|
-- declare
|
|
|
|
-- Pivot_Value : Element := A (A'First);
|
|
|
|
-- Right : Index := A'Last;
|
|
|
|
-- Left : Index := A'First;
|
|
|
|
-- begin
|
|
|
|
-- loop
|
|
|
|
-- while Left < Right and not (Pivot_Value < A (Left)) loop
|
|
|
|
-- Left := Index'Succ (Left);
|
|
|
|
-- end loop;
|
|
|
|
-- while Pivot_Value < A (Right) loop
|
|
|
|
-- Right := Index'Pred (Right);
|
|
|
|
-- end loop;
|
|
|
|
-- exit when Right <= Left;
|
|
|
|
-- Swap (Left, Right);
|
|
|
|
-- Left := Index'Succ (Left);
|
|
|
|
-- Right := Index'Pred (Right);
|
|
|
|
-- end loop;
|
|
|
|
-- if Right = A'Last then
|
|
|
|
-- Right := Index'Pred (Right);
|
|
|
|
-- Swap (A'First, A'Last);
|
|
|
|
-- end if;
|
|
|
|
-- if Left = A'First then
|
|
|
|
-- Left := Index'Succ (Left);
|
|
|
|
-- end if;
|
|
|
|
-- Quick_Sort (A (A'First .. Right));
|
|
|
|
-- Quick_Sort (A (Left .. A'Last));
|
|
|
|
-- end;
|
|
|
|
-- end if;
|
2020-12-22 19:03:03 +00:00
|
|
|
-- end Quick_Sort;
|
2020-12-23 13:43:03 +00:00
|
|
|
>>>>>>> .r210345
|