Nama : ANDI KHOIRUDIN
Kelas : 2-B SMT 2
Jurusan : Teknik Informatika
STIMIK YADIKA BANGIL 2015
Membuat Program 'Quick Sort' Sederhana dengan DELPHI
Langkah-langkahnya :
1. Buka Aplikasi Delphi
2. Pilih Menu File --> New --> Form Delphi
unit UContohh;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, sSkinManager;
type
TFQuickSort = class(TForm)
Label1: TLabel;
Label3: TLabel;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
ListBox1: TListBox;
ListBox2: TListBox;
Button3: TButton;
Label2: TLabel;
Label4: TLabel;
Button4: TButton;
Label5: TLabel;
sSkinManager1: TsSkinManager;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(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;
procedure TFQuickSort.Button4Click(Sender: TObject);
begin
ListBox1.Items.Clear;
ListBox2.Items.Clear;
END;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, sSkinManager;
type
TFQuickSort = class(TForm)
Label1: TLabel;
Label3: TLabel;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
ListBox1: TListBox;
ListBox2: TListBox;
Button3: TButton;
Label2: TLabel;
Label4: TLabel;
Button4: TButton;
Label5: TLabel;
sSkinManager1: TsSkinManager;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(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;
procedure TFQuickSort.Button4Click(Sender: TObject);
begin
ListBox1.Items.Clear;
ListBox2.Items.Clear;
END;
end.
5. Terakhir coba jalankan program dengan tekan F9 atau klik tombol Run , apabila tidak ada pesan kesalahan maka program akan terlihat seperti di bawah ini :
6. Ketika program di coba untuk digunakan maka akan tampil seperti di bawah ini :
Jika ada kesalahan Mohon maaf dan terima kasih sudah berkunjung