comp.lang.ada
 help / color / mirror / Atom feed
From: Mehdi Saada <00120260a@gmail.com>
Subject: "begginner", sorting algorithm.
Date: Fri, 29 Jan 2021 06:57:02 -0800 (PST)	[thread overview]
Message-ID: <dfbe20ec-c344-497e-bb82-5a283fab24e5n@googlegroups.com> (raw)

 I'm starting again after years and very different studies.
This is a sorting algorithm for already ordered arrays, but with (many) duplicated elements. Of course it doesn't work instantly...
Please be sweet and help me figure out what goes wrong.

Principle :
IndiceA_1 and IndiceB_1 starts at 1.
For each array there is a loop between IndiceA_1+1 and the last array superior limit index, which stops when IndiceX2 is chosen as the last index for which Tx(IndiceX1) = Tx(IndiceX2). The loop is exited when the equality holds no more, and we get to the loop for the other array.
I want to obtain IndiceA_1, IndiceA_2, IndiceB_1, IndiceB_2 so that depending on order both data array slices, slices on data arrays are added to the result array with 
if TA(IndiceA_1) <= TB(IndiceB_1)
then
TC(IndiceC..IndiceC+IndiceA_2+IndiceB_2-IndiceA_1-IndiceA_2 + 1) := TA(IndiceA_1..IndiceA_2) & TA(IndiceB_1..IndiceB_2)
else
TC(IndiceC..IndiceC+IndiceA_2+IndiceB_2-IndiceA_1-IndiceA_2 + 2) := TA(IndiceB_1..IndiceB_2) & TA(IndiceA_1..IndiceA_2);
end if;

The indices are adjusted accordingly with :
IndiceC := IndiceC + IndiceA_2 + IndiceB_2 - IndiceA_1 - IndiceA_2 + 2;
IndiceB_1 := IndiceB_2 + 1;
IndiceA_1 := IndiceA_2 + 1;

And the main loop starts again.
The end is a finish… the programs doesn’t go there yet.

Here's it all.

with Ada.Text_IO; use Ada.text_io;
procedure Main is
   type Tableau is array(Positive range <>) of Integer;
   procedure Tri_Tableaux_ordonnés (TA, TB: in Tableau; TC: out Tableau) is
      IndiceA_1, IndiceA_2 : Positive range Ta'Range := TA'First;
      IndiceB_1, IndiceB_2 : Positive range Tb'Range := TB'First;
      IndiceC: Positive range 1..TC'Last+1 := TC'First;
      Fini_A, Fini_B: Boolean;
   begin
      loop
         for Asub in IndiceA_1+1..TA'Last loop
            if TA(Asub) /= TA(IndiceA_1) then
               IndiceA_2 := (if Asub = TA'Last - 1 then TA'Last else Asub-1);
               exit;
            end if;
            Put_Line("IndiceA_1 : " & IndiceA_1'Image);
            Put_Line("IndiceA_2 : " & IndiceA_2'Image);
         end loop;
         for Bsub in IndiceB_1+1..TB'Last-1 loop
            if TB(Bsub) /= TB(IndiceB_1) then
               IndiceB_2 := (if Bsub = TB'Last - 1 then TB'Last else Bsub-1);
               exit;
            end if;
            Put_Line("IndiceB_1 : " & IndiceB_1'Image);
            Put_Line("IndiceB_2 : " & IndiceB_2'Image);
         end loop;
         if TA(IndiceA_1) <= TB(IndiceB_1) then
            TC(IndiceC..IndiceC+IndiceA_2+IndiceB_2-IndiceA_1-IndiceA_2 + 2)
              := TA(IndiceA_1..IndiceA_2) & TA(IndiceB_1..IndiceB_2);
         else
            TC(IndiceC..IndiceC+IndiceA_2+IndiceB_2-IndiceA_1-IndiceA_2 + 2)
              := TA(IndiceB_1..IndiceB_2) & TA(IndiceA_1..IndiceA_2);
         end if;

         if IndiceA_2 = TA'Last then Fini_A := True;
         elsif IndiceB_2 = TB'Last then Fini_B := True;
         end if;
         IndiceA_1 := IndiceA_2 + 1;
         IndiceB_1 := IndiceB_2 + 1;
         IndiceC := IndiceC+IndiceA_2+IndiceB_2-IndiceA_1-IndiceA_2 + 3;
         exit when Fini_A or Fini_B;
      end loop;
      if not Fini_A and Fini_B then
         if Fini_B then
            TC(IndiceC..Tc'Last) := TA(IndiceA_1..TA'Last);
         elsif Fini_A then
            TC(IndiceC..Tc'Last) := TB(IndiceB_1..TB'Last);
         end if;
      end if;
    pragma Assert(IndiceC > TC'Last);
   end Tri_Tableaux_ordonnés;
   A: Tableau := (1, 3, 9, 11);
   B: Tableau := (5, 55, 99);
   C: Tableau(1..A'Length+B'Length);
begin
   Tri_Tableaux_ordonnés(TA => A, 
                          TB => B,
                          TC => C);
   for element of C loop
      Put_Line(element'Image);
   end loop;
end Main;

             reply	other threads:[~2021-01-29 14:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-29 14:57 Mehdi Saada [this message]
     [not found] ` <7b602137-c6b9-498a-a9e3-e6d98ff26b64n@googlegroups.com>
2021-01-30 11:27   ` "begginner", sorting algorithm Mehdi Saada
     [not found]   ` <478ab4aa-3176-4221-a1ca-13151fa5edd2n@googlegroups.com>
2021-01-30 23:54     ` Mehdi Saada
2021-02-05 15:56 ` robin
2021-02-05 17:12   ` Mehdi Saada
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox