Sabtu, 04 April 2015

searching

PROGRAM SEARCHING;



program pencarian2;
uses crt;
var lg :string;
ask:integer;

procedure sequential;
type
arrint = array [1..2000] of integer;
var
x      : integer;
tabint :arrint;
n,i    : integer;
indeks : integer;
function seqsearch1(xx : integer): integer;
var i : integer;
begin
i := 1;
while ((i<n) and (tabint[i] <> xx)) do
i:=i+1;
if tabint[i] = xx then
        seqsearch1:=i
else
        seqsearch1:=0;
end;

begin
clrscr;
write('Masukanbanyak data = '); readln(n);
for i:=1 to n do
begin
write('Angka ke [',i,'] = '); readln(tabint[i]);
end;
write('Nilai yang dicari = '); readln(x);
indeks:=seqsearch1(x);
if indeks<> 0 then
write(x,' ditemukan pada indekske :',indeks)
else
write(x,' tidak ditemukan');
readln;;
end;

procedure binary;
type arrint = array [1..1000] of integer;
var x : integer;
a :arrint;
n,i,j,z : integer;
indeks: integer;
function binarysearch (xx : integer): integer;
var i : integer;
atas,bawah,tengah : integer;
ditemukan :boolean;
indexx : integer;
begin
atas := 1;
bawah := n;
ditemukan :=false;
indexx:=0;
while ((atas<=bawah) and (not ditemukan)) do
begin
tengah :=(atas+bawah) div 2;
if xx = a[tengah] then
begin
ditemukan:=true;
indexx := tengah;
end
else
begin
if xx<a[tengah] then
bawah := tengah - 1
else
atas:=tengah + 1;
end;
end;

binarysearch:=indexx;
end;

begin
clrscr;
write('Masukan banyak data : ');
readln(n);
for i := 1 to n do
begin
write ('angka ke [',i,'] = ');
readln(a[i]);
end;
begin
for i:=1 to n-1 do
begin
for j:=n downto i+1 do
begin
if a[j] < a[j-1] then
begin
z:=a[j];
a[j]:=a[j-1];
a[j-1]:=z;
end;
end;
end;
end;

writeln;
write('data setelah diurutkan : ');
for j:=1 to n do
write(a[j],' ');

writeln;
write('nilai yang dicari : ');
readln(x);
indeks:=binarysearch(x);
if indeks<> 0 then
write(x,' ditemukan pada indekske : ',indeks)
else
write(x,' tidak ditemukan ');
readln;
end;


begin
repeat
clrscr;
writeln;
writeln('  Program Pencarian Data');

writeln;
writeln('==========================');
writeln('|        M E N U         |');
writeln('|                        |');
writeln('|------------------------|');
writeln('|(1) Sequential Search   |');
writeln('|(2) Binary Search       |');
writeln('|(3) Keluar              |');
writeln('|------------------------|');
write(' Pilih = ');readln(ask);
clrscr;
case ask of
1 : begin
sequential;
end;
2 : begin
binary;
end;
3 : begin
exit;
end;
end;
until(ask=3);
end.




SCREENSHOT:






0 komentar:

Posting Komentar