Sorting algorithms/Insertion sort: Difference between revisions

(→‎{{header|Pascal}}: added example)
Line 3,870:
 
=={{header|Pascal}}==
{{works with|FPC}}
See [[Sorting_algorithms/Insertion_sort#Delphi | Delphi]]
<syntaxhighlight lang="pascal">
program SortDemo;
 
{$mode objfpc}{$h+}{$b-}
 
procedure InsertionSort(var A: array of Integer);
var
I, J, Tmp: Integer;
begin
for I := 1 to High(a) do
if A[I] < A[I - 1] then begin
J := I;
Tmp := A[I];
repeat
A[J] := A[J - 1];
Dec(J);
until (J = 0) or (Tmp >= A[J - 1]);
A[J] := Tmp;
end;
end;
 
procedure PrintArray(const A: array of Integer);
var
I: Integer;
begin
Write('[');
for I := 0 to High(A) do
if I < High(A) then Write(A[I], ', ')
else Write(A[I]);
WriteLn(']');
end;
 
var
a: array[-7..6] of Integer = (-34, -20, 30, 13, 36, -10, 5, -25, 9, 19, 35, -50, 29, 11);
 
begin
InsertionSort(a);
PrintArray(a);
readln;
end.
</syntaxhighlight>
{{out}}
<pre>
[-50, -34, -25, -20, -10, 5, 9, 11, 13, 19, 29, 30, 35, 36]
</pre>
 
=={{header|Perl}}==
73

edits