Busqueda dicotomica
Publicado por supernino (1 intervención) el 08/04/2014 09:40:23
Lo he ido arreglando hasta tal punto que me encuentra las cosillas de la array, pero cuando le meto un número fuera de dicha array no hace nada, no digo que me lo arregléis, pero alguien me podría orientar un poco?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
program Busca_Recur;
program Busca_Recur;
uses crt;
const
max=15;
vecto:array[1..max] of integer=(4,6,9,11,13,23,44,45,45,56,66,66,67,77,88);
type
vecto1=array[1..max] of integer;
var
dato,cen,resul:integer;
control:boolean;
function Buscar(N:vecto1;ini,fin:integer):integer;
begin
control:=TRUE;
// writeln('INI_FIN :',ini,' - ',fin);delay(500);
While control AND (ini<=fin) DO BEGIN
cen:=(ini+fin) div 2;
if (N[cen]=dato) then begin
Buscar:=dato;
control:=false;
end else begin
if (dato<N[cen]) then begin Buscar:=Buscar(N,ini,cen-1);
end else begin Buscar:=Buscar(N,cen+1,fin);
end;
end;
end;
end;
begin
write ('Da Valor :');
readln (dato);
resul:=Buscar(vecto,1,max);
if (resul=-121) then writeln('Dato no Valido :')
else writeln ('el resultado es Valido : ',resul,' Posi : ',cen);
readln;
end.
Valora esta pregunta


0