Insertion sort in pascal

0

I have to implement the insert sorting method in Pascal.

The problem is that in my algorithm I'm having to do multiple if s. One for every possibility.

I wonder if there is a better way to do this.

My code:

Program insertion_sort ;
var
    cont , aux : integer ;
    numero : array[1..5] of integer ;  
Begin
    for cont := 1 to 5 do
        begin
            writeln('Digite um numero : ');
            read(numero[cont]);
        end;

    for cont := 1 to 5 do
        begin
            if(numero[1] > numero[2]) then
                begin
                    aux := numero[1] ;
                    numero[1] := numero[2] ;
                    numero[2] := aux;
                end;
            writeln(numero[cont]);
        end;
End.
    
asked by anonymous 08.11.2017 / 19:26

1 answer

0

The solution was given in the English version:

link

Procedure InsertionSort(numbers : Array of Integer; size : Integer);
Var i, j, index : Integer


Begin
 For i := 2 to size-1 do
  Begin
   index := numbers[i];
   j := i;
   While ((j > 1) AND (numbers[j-1] > index)) do
    Begin
     numbers[j] := numbers[j-1];
     j := j - 1;
    End;
   numbers[j] := index;
  End;

The link has an example of how to use it.

    
22.05.2018 / 15:33