comp.lang.ada
 help / color / mirror / Atom feed
* "begginner", sorting algorithm.
@ 2021-01-29 14:57 Mehdi Saada
       [not found] ` <7b602137-c6b9-498a-a9e3-e6d98ff26b64n@googlegroups.com>
  2021-02-05 15:56 ` robin
  0 siblings, 2 replies; 5+ messages in thread
From: Mehdi Saada @ 2021-01-29 14:57 UTC (permalink / 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;

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: "begginner", sorting algorithm.
       [not found] ` <7b602137-c6b9-498a-a9e3-e6d98ff26b64n@googlegroups.com>
@ 2021-01-30 11:27   ` Mehdi Saada
       [not found]   ` <478ab4aa-3176-4221-a1ca-13151fa5edd2n@googlegroups.com>
  1 sibling, 0 replies; 5+ messages in thread
From: Mehdi Saada @ 2021-01-30 11:27 UTC (permalink / raw)


I'm changing everything. Simplifying. Putting into subroutines the chunk making and others bits.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: "begginner", sorting algorithm.
       [not found]   ` <478ab4aa-3176-4221-a1ca-13151fa5edd2n@googlegroups.com>
@ 2021-01-30 23:54     ` Mehdi Saada
  0 siblings, 0 replies; 5+ messages in thread
From: Mehdi Saada @ 2021-01-30 23:54 UTC (permalink / raw)


Now I have this weird bug report:

gprbuild -d -P/home/mehdi/ordonned_arrays_sort.gpr
gprbuild -d -P/home/mehdi/ordonned_arrays_sort.gpr
Compile
   [Ada]          ordonned_arrays_sort_main.adb
+===========================GNAT BUG DETECTED==============================+
| 8.3.0 (x86_64-linux-gnu) in force_constant_size, at gimplify.c:687       |
| Error detected around /home/mehdi/src/ordonned_arrays_sort_main.adb:47:16|
| Please submit a bug report; see https://gcc.gnu.org/bugs/ .              |
| Use a subject line meaningful to you and us to track the bug.            |
| Include the entire contents of this bug box in the report.               |
| Include the exact command that you entered.                              |
| Also include sources listed below.                                       |
+==========================================================================+

Please include these source files with error report
Note that list may not be accurate in some cases,
so please double check that the problem can still
be reproduced with the set of files listed.
Consider also -gnatd.n switch (see debug.adb).

/home/mehdi/src/ordonned_arrays_sort_main.adb

raised TYPES.UNRECOVERABLE_ERROR : comperr.adb:407
gprbuild: *** compilation phase failed
[2021-01-30 23:48:27] process exited with status 4, elapsed time: 01.53s
Compile
   [Ada]          ordonned_arrays_sort_main.adb

[Exact same GNAT BUG DETECTED as above]
[...]
Consider also -gnatd.n switch (see debug.adb).

/home/mehdi/src/ordonned_arrays_sort_main.adb

raised TYPES.UNRECOVERABLE_ERROR : comperr.adb:407
gprbuild: *** compilation phase failed
[2021-01-30 23:48:27] process exited with status 4, elapsed time: 01.46s
Traceback (most recent call last):
  File "/usr/share/gps/support/ui/gnattest.py", line 233, in open_harness_filter
    return os.path.exists(get_harness_project_file(project))
  File "/usr/share/gps/support/ui/gnattest.py", line 103, in get_harness_project_file
    return six.reduce(compare, list, "")
AttributeError: 'module' object has no attribute 'reduce'

__________________________________
the line mentioned is "index1 := 2;", index1 being Natural.

I uninstalled/reinstalled gnat (same version, from debian stable repository) several time. bug stays. but only on this file so far.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: "begginner", sorting algorithm.
  2021-01-29 14:57 "begginner", sorting algorithm Mehdi Saada
       [not found] ` <7b602137-c6b9-498a-a9e3-e6d98ff26b64n@googlegroups.com>
@ 2021-02-05 15:56 ` robin
  2021-02-05 17:12   ` Mehdi Saada
  1 sibling, 1 reply; 5+ messages in thread
From: robin @ 2021-02-05 15:56 UTC (permalink / raw)



"Mehdi Saada" <00120260a@gmail.com> wrote in message 
news:dfbe20ec-c344-497e-bb82-5a283fab24e5n@googlegroups.com...
> 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.

For an already-ordered array, you don't need a sorting algorithm.

What is it that you are trying to achieve? 



---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: "begginner", sorting algorithm.
  2021-02-05 15:56 ` robin
@ 2021-02-05 17:12   ` Mehdi Saada
  0 siblings, 0 replies; 5+ messages in thread
From: Mehdi Saada @ 2021-02-05 17:12 UTC (permalink / raw)


It was about fusing - in an ordered fashioned - two ordered arrays. Now it's done, I calmed myself and figured out.
what helped was making independent subroutines out of chunks of instructions, to see where the craziness would start. Powerful method ;-)

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-02-05 17:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-29 14:57 "begginner", sorting algorithm Mehdi Saada
     [not found] ` <7b602137-c6b9-498a-a9e3-e6d98ff26b64n@googlegroups.com>
2021-01-30 11:27   ` 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

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