Minggu, 21 Juni 2015

Pengurutan data menggunakan Quick Sort



List Program 1
List program 2


List Program
unit quicksorta;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TFQuickSort = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    ListBox1: TListBox;
    ListBox2: TListBox;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
    Data:array of integer;
  public
    { Public declarations }
  end;

var
  FQuickSort: TFQuickSort;

implementation

{$R *.dfm}

procedure QuickSort(var A:array of integer;iLo,iHi:integer);
var
  Lo,Hi,Pivot,T:integer;
begin
  Lo:=iLo;
  Hi:=iHi;
  Pivot:=A[(Lo+Hi) div 2];
  repeat
    while A[Lo]<Pivot do Inc(Lo);
    while A[Hi]>Pivot do Dec(Hi);
    if Lo<=Hi then
    begin
      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);
end;

procedure TFQuickSort.Button1Click(Sender: TObject);
var
  nilai:integer;
begin
  //menambahkan nilai ke listbox
  if TryStrToInt(Edit1.Text,Nilai) then
  ListBox1.Items.Add(IntToStr(nilai));
  //membersihkan komponen inputan nilai
  Edit1.Clear;
  //focus kursor ke inputan nilai
  Edit1.SetFocus;
end;

procedure TFQuickSort.Button2Click(Sender: TObject);
var
  indek:Word;
begin
  //menentukan panjang array
  SetLength(Data,ListBox1.Items.Count);

  //pemasukan nilai ke array
  for indek:=0 to ListBox1.Items.Count-1 do
  Data[indek]:=StrToInt(ListBox1.Items[indek]);
  //proses pengurutan/sorting
  QuickSort(data,Low(Data),High(Data));
  //kosongkan hasil
  ListBox2.Items.Clear;
  //memasukkan nilai hasil
  for indek:=0 to ListBox1.Items.Count-1 do
  ListBox2.Items.Add(IntToStr(data[indek]));
end;

procedure TFQuickSort.Button3Click(Sender: TObject);
begin
  close;
end;

end.

Hasil Running













0 komentar:

Posting Komentar

Categories

Total Tayang

Jam

Kalender Islam

Diberdayakan oleh Blogger.

Popular Posts

Pengikut