Вы находитесь на странице: 1из 5

This article is about the programming algorithm.

For the technique for finding extremum of a mathematical function, see Golden section search. The Fibonacci search technique is a method of searching a sorted array using a divide and conquer algorithm that narrows down possible locations with the aid of Fibonacci numbers. Compared to binary search, Fibonacci search examines locations whose addresses have lower dispersion. Therefore, when the elements being searched have non-uniform access memory storage (i.e., the time needed to access a storage location varies depending on the location previously accessed), the Fibonacci search has an advantage over binary search in slightly reducing the average time needed to access a storage location. The typical example of nonuniform access storage is that of a magnetic tape, where the time to access a particular element is proportional to its distance from the element currently under the tape's head. Note, however, that large arrays not fitting in cache or even in RAM can also be considered as non-uniform access examples. Fibonacci search has a complexity of O(log(x)) (see Big O notation).

What is Fibonacci Numbers? By definition in mathematics, the Fibonacci Numbers are the numbers in the below sequence:

0,1,1,2,3,5,8,13,21,...........

By definition, the first Fibonacci number is 0 and second Fibonacci number is 1. Then each subsequent number is calculated based on sum of the previous two. In Mathematics, this is also called recurrence relation.

//Write a program that use both recursive and non-recursive functions to perform Fibonacii search for a key. #include<stdio.h> #include<conio.h> int fib(int a); main() {

static int i; int k,ch,temp; int key,pre=0,cur=1,flag=0; //variables decleration clrscr(); // Clear the previous data on the Screen printf("\n1.Non-Recursive search\n 2.Recursive search"); printf("\nEntr your choice:"); scanf("%d",&ch); if(ch>0&&ch<=2) { printf("\nEnter key value:"); scanf("%d",&key);//Read n value from the keyboard/user } switch(ch) { case 1:printf("Fibonacii series upto %d numbers using non-recursive is:\n",key); printf("%d\t%d",pre,cur); if(key==0) { printf("\nkey is found at 0 position"); break; } else {

for(i=1;i<=key;i++) { temp=cur+pre; printf("\t%d",temp); if(temp==key) { flag=1; break; } pre=cur; cur=temp; if(key<temp) break; } if(flag==1) printf("\nKey is found at %d position",i+1); else printf("\nKey not found"); break; }

case 2:printf("Fibonacii series upto %d numbers using rucursive is:\n",key); for(i=0;i<=key+1;i++)//for loop {

k=fib(i); if(key==k) { printf("%d\t",k); flag=1; break; } else if(key<k) { printf("%d\t",k); break;

printf("%d\t",k); //calling the "fib()" function } if(flag==1) printf("\nkey is found at %d position",i); else printf("\nkey is not found"); break; default:printf("choose 1 or 2 only"); }

getch(); }

int fib(int a) //called funtion { if(a==0) // Checking if condtion return 0; // if the condition is true ie a=0 then return value is 0 if(a==1) return 1; // If the condition is true ie a=1 thenn return value is 1 else return (fib(a-1)+fib(a-2)); // calling fib() funtion itself }

/*output: Enter n value: 6 Fibonacii series upto 6 numbers is: 0 1 1 2 3 5 */

Вам также может понравиться