//使用如下的方法,可以对任意大小的数组按照LZ的排序规则排序
procedure jh(pi: PInteger;
n, iCount, iEnd: Integer);
var
tmp: Integer;
pi2: PInteger;
begin
pi2 := pi;
if n mod 2 = 1 then
begin
if iCount mod 2 = 1 then
tmp := (iCount - n) div 2
else
tmp := (iCount - 1 - n) div 2;
Inc(pi2, tmp);
tmp := tmp + n;
if tmp <> iEnd then
jh(pi2, tmp, iCount, iEnd)
end
else
begin
tmp := n div 2;
Dec(pi2, tmp);
if tmp <> iEnd then
jh(pi2, tmp, iCount, iEnd)
end;
if n <> iEnd then
pi2^ := pi^;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
a: array of Integer;
i: Integer;
k: Integer;
iCount: Integer;
begin
try
iCount := StrToInt(Edit1.Text);
except
iCount := 100;
end;
//初始化数组,为了方便计算,第0个数组不用
SetLength(a, iCount);
for i := 0 to iCount - 1do
a := i + 1;
//排序:奇数在前,偶数在后,并保持顺序
i := 1;
while i < ((iCount + 1) div 2)do
begin
if a mod 2 = 0 then
begin
k := a;
jh(@a, i, iCount, i);
if (iCount mod 2) = 0 then
a[i + (iCount - 1 - i) div 2] := k
else
a[i + (iCount - i) div 2] := k;
end;
Inc(i)
end;
//输出
Memo1.Lines.begin
Update;
try
Memo1.Clear;
for i := 0 to iCount - 1do
Memo1.Lines.Add(IntToStr(a))
finally
Memo1.Lines.EndUpdate;
end;
end;