This code permutes an integer array, but I'm not sure if it is optimal (probably isn't).
type
TDynIntegerArray = array of integer;
procedure PermuteArray(A: TDynIntegerArray);
var
B: TDynIntegerArray;
Z: TDynIntegerArray;
π: TDynIntegerArray;
i: Integer;
j: Integer;
k: Integer;
begin
B := Copy(A);
SetLength(Z, Length(A));
SetLength(π, Length(A));
for i := 0 to High(Z) do
Z[i] := i;
for i := 0 to High(π) do
begin
π[i] := RandomFrom(Z);
for j := 0 to High(Z) do
begin
if Z[j] = π[i] then
begin
for k := j to High(Z) - 1 do
Z[k] := Z[k+1];
SetLength(Z, length(Z) - 1);
break;
end;
end;
end;
for i := 0 to High(A) do
A[i] := B[π[i]];
end;
A much faster, but less cool approach is simply to randomly swap the items, one pair at a time:
procedure FastPermuteArray(A: TDynIntegerArray);
procedure Swap(n, m: integer);
var
tmp: integer;
begin
tmp := A[n];
A[n] := A[m];
A[m] := tmp;
end;
var
i: Integer;
begin
for i := High(A) downto 1 do
Swap(i, RandomRange(0, i));
end;