LISTING PROGRAM:
program pencarian;
uses crt;
const maks = 100;
type larikint = array[1..maks] of integer;
var
i:integer;
x:integer;
l:larikint;
idx:integer;
procedure isi_larik(var l:larikint; i:integer);
var
p:integer;
begin
for p:= 1 to i do
begin
write('nilai elemen',p,' : ');readln(l[p]);
end;
end;
procedure binary_search(l:larikint; i:integer; x:integer; var idx:integer);
var
p,y:integer;
m:integer;
ketemu:boolean;
begin
p:= 1;
y:= i;
ketemu:= false;
while (not ketemu) and (p <= y) do
begin
m:=(p+y)div 2;
if (l[m] = x) then
ketemu:=true
else
if (l[m]<x) then
p:=m+1
else
y:=m-1;
end;
if (ketemu) then
idx:=m
else
idx:=-1;
end;
begin
write('banyaknya elemen larik : ');readln(i);
isi_larik(l,i);
write('elemen yang akan dicari : ');readln(x);
binary_search(l,i,x,idx);
if idx<>-1 then
write(x,' sudah terdapat di dalam larik')
else
write(x,' tidak terdapat di dalam larik');
readln();
end.
OUTPUT
MY PROFILE
- Mistin Herawati
- Depok, Jawa Barat, Indonesia
- AC MILAN | MILANISTI | KEVIN-PRINCE BOATENG FANS
WHAT TIME IS IT?
MY FOLLOWERS
STATISTICS OF MY BLOG
Copyright by: Mistin Herawati. Diberdayakan oleh Blogger.
Jumat, 13 April 2012
Binary Search Bahasa Pascal
Diposting oleh Mistin Herawati di 11.48.00
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar