0

I need to create an array within a register that is a sequence of natural numbers, these are inserted by the user and cannot be repeated. My problem is sorting the array values as they are inserted in ascending order. I have tried this code without success. Thank you.

const
  MAX = 10;
type
  Natural = 0..MAXINT; 

  Secuencia = RECORD
    valores : ARRAY [1..MAX] OF Natural;
    tope : 0..MAX;
  END;
  
TipoResultado = (Fallo, Creado, Agregado);
  
Resultado = RECORD
    CASE quePaso : TipoResultado OF
    Fallo: ();
    Creado: ();
    Agregado: (posicion: Natural);
  END;


//Non-repeating value search function in array
Function BLineal(valor: Natural; sec: Secuencia ): boolean;
var i : integer;
begin
  i := 1;
  while (i <= sec.tope) and (sec.valores[i] <> valor) do
    i := i + 1;
     BLineal := i <= sec.tope
    end;

Procedure to order values within the arrangement from smallest to largest by insertion.

//Procedure ORDER 
Procedure OrdIns (var sec: Secuencia);
var
   i,j: integer;
   aux: Natural;
begin
  for i := 2 to sec.tope do begin
    j := i;
    while (j >= 2) and ((sec.valores[j]) < (sec.valores[j-1])) do
      begin
        aux:=sec.valores[j-1];
          sec.valores[j-1]:=sec.valores[j];
            sec.valores[j]:=aux;
              j := j - 1
         end
    end;
end;
  • Sorry, you're right. The user only enters the natural values. I already modified the question. Thank you – Richard Brancato Jun 20 '20 at 18:07
  • 2
    I have deleted my previous comment. If I were performing this task, I would use a [binary search](https://en.wikipedia.org/wiki/Binary_search_algorithm) to locate the correct insertion point. – MartynA Jun 20 '20 at 18:31

1 Answers1

0

Do the problem in two steps. First, create a binary tree and insert values as entered. Then when no more values are found simply walk the tree and create the array.

kd4ttc
  • 1,075
  • 1
  • 10
  • 28