unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); procedure ShellSort(n :integer); private { Private declarations } public { Public declarations } end; var Form1: TForm1; AnArray :array[1..20] of integer; implementation {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject); var i, j : integer; begin {put a bunch of numbers into the array} Randomize; j := (sizeof(AnArray)) div (sizeof(integer)); for i := 1 to j do AnArray[i] := Random(100); ShellSort(j); {pass array num elements to shellsort} For i := 1 to j do {visual debugging} Memo1.Lines.Append(IntToStr(AnArray[i])); end; procedure TForm1.ShellSort(n : integer); {n = num elements in array} label Jumpout; var g, h, i, j, value :integer; s : string; begin {visual debugging code, watch selection sort on memo comment this code out for large array sizes} s := ''; for g := 1 to n do s := s + IntToStr(AnArray[g]) + ' '; Memo1.Lines.Append(s); {find the most efficient value for h} h := 1; repeat h := (3 * h) + 1; until (h > n); {shell sort} repeat h := h div 3; for i := h + 1 to n do begin value := AnArray[i]; j := i; while (AnArray[j - h] > value) do begin AnArray[j] := AnArray[j - h]; j := j - h; if j <= h then goto Jumpout; end; Jumpout : AnArray[j] := Value; end; {visual debugging code, watch selection sort on memo comment this code out for large array sizes} s := ''; for g := 1 to n do s := s + IntToStr(AnArray[g]) + ' '; Memo1.Lines.Append(s); until (h = 1); end; end.