Compare with the two Rosetta code entries?
rosettacode.org/wiki/Sorting_algorithms/Merge_sort#Pascal
Note that with both examples and the size of your array, you are not testing mergesort, but insertion sort.
Anyway, I found it fun, so I wrote a version from scratch that can be proven stable:
program SortOfAnIssue;
{$mode objfpc}{$H+}
uses types;
procedure MergeSort(var A: TIntegerDynArray; const Buf: TIntegerDynArray; const Left, Right: Integer);inline;overload;
var
Center, I, J, K: Integer;
begin
if Right - Left > 1 then
begin
Center := (Left + Right) div 2;
MergeSort(A, Buf, Left, Center);
MergeSort(A, Buf, Center, Right);
for I := Left to Center - 1 do
Buf[I] := A[I];
I := Left;
J := Center;
K := Left;
while I < Center do
begin
if J >= Right then
A[K] := Buf[I]
else if Buf[I] < A[J] then
A[K] := Buf[I]
else
A[K] := A[J];
Inc(I);
Inc(K);
if J < Right then
Inc(J);
end;
end
else if Right - Left = 1 then // adjust here, can be anything small that suits you.
begin
// Insertion Sort for small sizes
J := Left;
while (J > 0) and (A[J-1] > A[J]) do
begin
I := A[J-1];
A[J-1] := A[J];
A[J] := I;
Dec(J);
end;
end;
end;
procedure MergeSort(var A: TIntegerDynArray);inline;overload;
begin
MergeSort(A, A, 0, Length(A));
end;
var
Items:TIntegerDynArray = { objfpc } (707,707,101); // mode delphi is: [707,707,101]; // square brackets
i:integer;
begin
mergesort(Items);
for i in items do writeln(i);
end.
That is, keeping the rosetta code entries in mind and hopefully some prior knowledge left-overs.
There is still room for improvement: fast fingers, slow mind.
May also be slow speed, but point was to prove stability.