Bug fixed. I checked the code, GetMem's code has no bug and it supports non-zero-based array. But you cannot simply copy/paste to use it, some adaptations are needed.
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs,
ExtCtrls, StdCtrls,Buttons,ComCtrls,
Menus, Types, LCLType, ActnList;
type
SouthHandArray = Array[1..13] of Integer;
{ TForm1 }
TForm1 = class(TForm)
btnGo: TButton;
ListBox1: TListBox;
ListBox2: TListBox;
procedure btnGoClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure QuickSort(var AI: SouthHandArray; ALo, AHi: Integer);
procedure ShowSouthHand;
private
public
end;
var
Form1: TForm1;
SouthHand : SouthHandArray;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.Height:=1050;
Form1.Width:=1552;
ListBox1.Width := 80;
end;
procedure TForm1.btnGoClick(Sender: TObject);
begin
Listbox1.Clear;
SouthHand[1]:=27;
SouthHand[2]:=17;
SouthHand[3]:=7;
SouthHand[4]:=52;
SouthHand[5]:=5;
SouthHand[6]:=49;
SouthHand[7]:=33;
SouthHand[8]:=29;
SouthHand[9]:=21;
SouthHand[10]:=14;
SouthHand[11]:=38;
SouthHand[12]:=3;
SouthHand[13]:=23;
QuickSort(SouthHand, 1, 13) ;
ShowSouthHand;
end;
procedure TForm1.QuickSort(var AI: SouthHandArray; ALo, AHi: Integer);
var
Lo, Hi, Pivot, T: Integer;
begin
Lo := ALo;
Hi := AHi;
Pivot := AI[(Lo + Hi) div 2];
repeat
while AI[Lo] < Pivot do
Inc(Lo) ;
while AI[Hi] > Pivot do
Dec(Hi) ;
if Lo <= Hi then
begin
T := AI[Lo];
AI[Lo] := AI[Hi];
AI[Hi] := T;
Inc(Lo) ;
Dec(Hi) ;
end;
until Lo > Hi;
if Hi > ALo then
QuickSort(AI, ALo, Hi) ;
if Lo < AHi then
QuickSort(AI, Lo, AHi) ;
end;
procedure TForm1.ShowSouthHand;
begin
Listbox1.Clear;
Listbox1.Items.add(IntToStr(SouthHand[1]));
Listbox1.Items.add(IntToStr(SouthHand[2]));
Listbox1.Items.add(IntToStr(SouthHand[3]));
Listbox1.Items.add(IntToStr(SouthHand[4]));
Listbox1.Items.add(IntToStr(SouthHand[5]));
Listbox1.Items.add(IntToStr(SouthHand[6]));
Listbox1.Items.add(IntToStr(SouthHand[7]));
Listbox1.Items.add(IntToStr(SouthHand[8]));
Listbox1.Items.add(IntToStr(SouthHand[9]));
Listbox1.Items.add(IntToStr(SouthHand[10]));
Listbox1.Items.add(IntToStr(SouthHand[11]));
Listbox1.Items.add(IntToStr(SouthHand[12]));
Listbox1.Items.add(IntToStr(SouthHand[13]));
end;
end.
The code above is the fixed version. I only made modifications on:
-
Line #24-
Line #70-
Line #46 (not important, only cosmetic issue on my system)
Lazarus/Pascal is strict but it is not strict enough to prevent you to supply wrong data type for the parameter. In this case, dynamic array vs static lenght array. And that was the cause of the issue.Edit:Actually the problem was you're trying to provide non-zero-based array as an open array. Open array always count from 0 but your array
(SouthHandArray) start from 1.
The code above solve the problem by using exact data type on the parameter. Alternatively, you can fix the problem simply changing the
line #65 in the code above to
QuickSort(SouthHand, 0, 12); , but keeping the open array in the
line #24 &
#70.