program binary_search;
uses crt;
var input:Array [1..30,1..3] of integer;
jumlah,i,j,cari,langkah:integer;
isi:boolean;
procedure mencari;
function cektengah(var z:integer):integer;
var x:integer;
ada: boolean;
begin
x := 1;
ada := false;
while not(ada) do
begin
if (z = input[x,2]) then
begin
cektengah := x;
ada := true;
end
else
x := x+1;
end;
end;
function cekkirkan(var z:integer): integer;
var x:integer;
ada: boolean;
begin
x := 1;
ada := false;
while not(ada) do
begin
if (z = input[x,1]) or (z =input[x,3]) then
begin
cekkirkan := x;
ada := true;
end
else
x := x+1;
end;
end;
begin
j := cektengah(cari); langkah := 1;
while (j > 1) do
begin
i := input[j,2]; j := cekkirkan(i);
if (input[j,1] = i) or (input[j,3] = i) then
begin
langkah := langkah + 1;
end;
end;
writeln('Angka ditemukan dalam ',langkah,' langkah');
end;
procedure masuk;
function cektengah(var z:integer):integer;
var x:integer;
ada: boolean;
begin
x := 1;
ada := false;
while not(ada) do
begin
if (z = input[x,2]) then
begin
cektengah := x;
ada := true;
end
else
x := x+1;
end;
end;
begin
j:=1;isi:=false;
while not(isi) do
begin
if (input[i,2]>input[j,2])then
begin
if (input[j,3]=0)then
begin
input[j,3]:=input[i,2];
isi:=true;
end
else j:=cektengah(input[j,3]);
end
else
begin
if (input[j,1]=0) then
begin
input[j,1]:=input[i,2];
isi:=true;
end
else j:=cektengah(input[j,1]);
end;
end;
end;
begin
clrscr;
writeln('***************************');
writeln(' Program Binary Search ');
writeln('***************************');
writeln('*****PROGRAM DIMULAI*******');
write('Masukan jumlah input : ');readln(jumlah);
for i:= 1 to jumlah do
begin
write('Masukkan elemen ke -',i,' : ');readln(input[i,2]);
input[i,1]:=0;input[i,3]:=0;
end;
for i:= 2 to jumlah do
begin
masuk;
end;
writeln('****************************');
write('Masukan angka yang dicari : ');readln(cari);
mencari;
writeln('***********************************');
writeln('Terima kasih atas partisipasi anda!');
writeln('*********PROGRAM SELESAI***********');
readln;
end.
No comments:
Post a Comment