⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit2.~pas

📁 JAVA线程表现方式
💻 ~PAS
字号:
unit Unit2;

interface

uses
  Classes, Graphics, ExtCtrls;

type
  { TSortThread}
  PsortArray = ^TSortArray;
  TSortArray = array[0..MaxInt div SizeOf(Integer)-1] of Integer;
  TSortThread = class(TThread)
  private
    { Private declarations }
    FBox:TPaintBox;
    FSortArray:PSortArray;
    FSize:Integer;
    FA,FB,FI,FJ:Integer;
    procedure DoVisualSwap;
  protected
    procedure Execute; override;
    Procedure VisualSwap(A,B,I,J:Integer);
    procedure Sort(var A:array of Integer);virtual;abstract;
  public
    Constructor Create(Box:TPaintBox; var SortArray:array of Integer);
  end;

  {TBubbleSort}       //冒泡

  TBubbleSort = class(TSortThread)
  protected
    procedure Sort(var A:array of Integer);override;
  end;

  {TselectionSort}       //选择

  TselectionSort = class(TSortThread)
  protected
    procedure Sort(var A:array of Integer);override;
  end;

  {TQuickSort}       //快速

  TQuickSort = class(TSortThread)
  protected
    procedure Sort(var A:array of Integer);override;
  end;

procedure PaintLine(Canvas:TCanvas;I,Len:Integer);

implementation

uses Unit1;

procedure PaintLine(Canvas:TCanvas;I,Len:Integer);
begin
  Canvas.Polyline([Point(0,I*2+1),Point(Len,I*2+1)]);
end;

{ TSortThread }

constructor TSortThread.Create(Box: TPaintBox;
  var SortArray: array of Integer);
begin
  FBox:=Box;
  FSortArray:=@SortArray;
  FSize:=High(SortArray) -Low(SortArray)+1;
  FreeOnTerminate:=True;
  inherited Create(False);
end;

procedure TSortThread.DoVisualSwap;
begin
  with FBox do
  begin
    Canvas.Pen.Color:=clBtnFace;
    PaintLine(Canvas,FI,FA);
    PaintLine(Canvas,FJ,FB);

    Canvas.Pen.Color:=clRed;
    PaintLine(Canvas,FI,FB);
    PaintLine(Canvas,FJ,FA);
  end;
end;

procedure TSortThread.VisualSwap(A, B, I, J: Integer);
begin
  FA:=A;
  FB:=B;
  FI:=I;
  FJ:=J;
  Synchronize(DoVisualSwap);
end;


procedure TSortThread.Execute;
begin
  { Place thread code here }
  Sort(Slice(FSortArray^,FSize));


end;


{ TBubbleSort }

procedure TBubbleSort.Sort(var A: array of Integer);
var
  I,J,T:Integer;
begin
  inherited;
  for I:=High(A) downto Low(A) do
    for J:=low(A) to I -1 do
      if a[j] > A[J+1] then
      begin
        VisualSwap(A[J],A[J+1],J,J+1);
        T:=A[J];
        A[J]:=A[J+1];
        A[J+1]:=T;
        if Terminated then exit;
      end;
end;

{ TselectionSort }

procedure TselectionSort.Sort(var A: array of Integer);
var
  I,J,T:Integer;
begin
  inherited;
  for I:=Low(A) to High(A)-1 do
    for J:=High(A) downto I+1 do
      if A[I]>A[J] then
      begin
        VisualSwap(A[I],A[J],I,J);
        T:=A[I];
        A[I]:=A[J];
        A[J]:=T;
        if Terminated then exit;
      end;
end;

{ TQuickSort }

procedure TQuickSort.Sort(var A: array of Integer);
  procedure QuickSort(var A:array of Integer;iLo,iHi:Integer);
  var
    Lo,Hi,Mid,T:Integer;
  begin
    inherited;
    Lo:=iLo;
    Hi:=iHi;
    Mid:=A[(Lo+Hi) div 2];
    repeat
      while A[Lo]<Mid do Inc(Lo);
      while A[Hi]>Mid do Dec(Hi);
      if Lo<=Hi then
      begin
        VisualSwap(A[Lo],A[Hi],Lo,Hi);
        T:=A[Lo];
        A[Lo]:=A[Hi];
        A[Hi]:=T;
        Inc(Lo);
        Dec(Hi);
      end;
    until Lo>Hi;
    if Hi>iLo then QuickSort(A,iLo,Hi);
    if Lo<iHi then QuickSort(A,Lo,iHi);
    if Terminated then Exit;
  end;

begin
  QuickSort(A,Low(A),High(A));
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -