busqueda de fibonacci
Publicado por amanda (1 intervención) el 30/04/2004 15:12:32
Hola, necesito presentar el algoritmo de la búsqueda de fibonacci en Pascal, es un tipo de búsqueda como la binaria, lo encontré en creo que es Ada, pero no se como trabajan las sentecias discrete, new, with, si alguien me puede ayudar desde ya lo agradezco.
aquí va lo que encontré:
Algorithm implementation
1 function fibonacci_search(item: integer; arr: sort_array) return index
2 is
3 l : index := arr'first; -- first element of array
4 u : index := arr'last; -- last element of array
5 m : index := (u+l)/2;
6 x,a,b : integer;
7 begin
8 a := (Fn-3);
9 b := (Fn-2)-(Fn-3);
10 discrete (f2,f1) := (Fn-2,Fn-3)
11 new (f2,f1) := (f2-f1,2*f1-f2) | (a,b)
12 with i := u-l+1
13 new i=i/2 loop
14 loop
15 if item < arr(m) then
16 m := m-f1; -- compute new position of compared element
17 f2 := f2-f1;
18 f1 := f1-f2;
19 elsif item > arr(m) then
20 m := m+f1; -- compute new position of compared element
21 x := f1;
22 f1 := f2-f1;
23 f2 := x;
24 a := f2; b := f1;
25 else
26 return m; -- return index of found item
27 end if;
28 i := i/2;
29 end loop;
30 end fibonacci_search;
aquí va lo que encontré:
Algorithm implementation
1 function fibonacci_search(item: integer; arr: sort_array) return index
2 is
3 l : index := arr'first; -- first element of array
4 u : index := arr'last; -- last element of array
5 m : index := (u+l)/2;
6 x,a,b : integer;
7 begin
8 a := (Fn-3);
9 b := (Fn-2)-(Fn-3);
10 discrete (f2,f1) := (Fn-2,Fn-3)
11 new (f2,f1) := (f2-f1,2*f1-f2) | (a,b)
12 with i := u-l+1
13 new i=i/2 loop
14 loop
15 if item < arr(m) then
16 m := m-f1; -- compute new position of compared element
17 f2 := f2-f1;
18 f1 := f1-f2;
19 elsif item > arr(m) then
20 m := m+f1; -- compute new position of compared element
21 x := f1;
22 f1 := f2-f1;
23 f2 := x;
24 a := f2; b := f1;
25 else
26 return m; -- return index of found item
27 end if;
28 i := i/2;
29 end loop;
30 end fibonacci_search;
Valora esta pregunta


0