Algoritmia - busqueda de fibonacci

 
Vista:

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;

Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Alejandro

Búsqueda de Fibonacci en Pascal adaptada

Publicado por Alejandro (307 intervenciones) el 01/03/2024 19:36:23
Amanda, aquí tienes una adaptación del algoritmo de búsqueda de Fibonacci en Pascal. Ten en cuenta que algunas de las funciones específicas de Ada, como `discrete`, `new`, y `with`, no son parte de Pascal. En cambio, he adaptado el código para que sea compatible con el Pascal estándar. Asegúrate de que los tipos y funciones se adapten a tu entorno Pascal.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
program FibonacciSearch;
 
type
  Index = Integer;
  SortArray = array of Integer;
 
function FibonacciSearch(item: Integer; arr: SortArray): Index;
var
  l, u, m, x, a, b, f1, f2, i: Integer;
begin
  l := 1; // first element of array
  u := Length(arr); // last element of array
  m := (u + l) div 2;
 
  a := 0;
  b := 1;
  f1 := 1;
  f2 := 1;
 
  while f2 <= (u - l + 1) do
  begin
    f2 := f1 + f2;
    f1 := f2 - f1;
  end;
 
  i := (u - l + 1) - f1;
 
  while i > 0 do
  begin
    if item < arr[m] then
    begin
      m := m - f1; // compute new position of compared element
      f2 := f2 - f1;
      f1 := f1 - f2;
    end
    else if item > arr[m] then
    begin
      m := m + f1; // compute new position of compared element
      x := f1;
      f1 := f2 - f1;
      f2 := x;
    end
    else
    begin
      Result := m; // return index of found item
      Exit;
    end;
 
    i := i div 2;
  end;
 
  Result := -1; // item not found
end;
 
var
  arr: SortArray;
  item, index: Integer;
 
begin
  // Ejemplo de uso
  arr := [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377];
  item := 21;
 
  index := FibonacciSearch(item, arr);
 
  if index <> -1 then
    writeln('Elemento ', item, ' encontrado en la posición ', index)
  else
    writeln('Elemento ', item, ' no encontrado en el array.');
end.

Este código implementa el algoritmo de búsqueda de Fibonacci en Pascal. Asegúrate de ajustar el código según tus necesidades y el entorno de desarrollo que estás utilizando.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar