2018-03-20 18:39:13 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2017-09-28 23:32:39 +03:00
|
|
|
int fibMonaccianSearch(int arr[], int x, int n)
|
|
|
|
{
|
|
|
|
/* Initialize fibonacci numbers */
|
2020-05-29 23:23:24 +03:00
|
|
|
int fibMMm2 = 0; // (m-2)'th Fibonacci No.
|
|
|
|
int fibMMm1 = 1; // (m-1)'th Fibonacci No.
|
|
|
|
int fibM = fibMMm2 + fibMMm1; // m'th Fibonacci
|
2018-03-20 18:39:13 +03:00
|
|
|
|
2017-09-28 23:32:39 +03:00
|
|
|
/* fibM is going to store the smallest Fibonacci
|
|
|
|
Number greater than or equal to n */
|
|
|
|
while (fibM < n)
|
|
|
|
{
|
|
|
|
fibMMm2 = fibMMm1;
|
|
|
|
fibMMm1 = fibM;
|
2020-05-29 23:23:24 +03:00
|
|
|
fibM = fibMMm2 + fibMMm1;
|
2017-09-28 23:32:39 +03:00
|
|
|
}
|
2018-03-20 18:39:13 +03:00
|
|
|
|
2017-09-28 23:32:39 +03:00
|
|
|
// Marks the eliminated range from front
|
|
|
|
int offset = -1;
|
2018-03-20 18:39:13 +03:00
|
|
|
|
2017-09-28 23:32:39 +03:00
|
|
|
/* while there are elements to be inspected. Note that
|
|
|
|
we compare arr[fibMm2] with x. When fibM becomes 1,
|
|
|
|
fibMm2 becomes 0 */
|
|
|
|
while (fibM > 1)
|
|
|
|
{
|
|
|
|
// Check if fibMm2 is a valid location
|
2018-03-20 18:39:13 +03:00
|
|
|
|
|
|
|
// sets i to the min. of (offset+fibMMm2) and (n-1)
|
2020-05-29 23:23:24 +03:00
|
|
|
int i = ((offset + fibMMm2) < (n - 1)) ? (offset + fibMMm2) : (n - 1);
|
2018-03-20 18:39:13 +03:00
|
|
|
|
2017-09-28 23:32:39 +03:00
|
|
|
/* If x is greater than the value at index fibMm2,
|
|
|
|
cut the subarray array from offset to i */
|
|
|
|
if (arr[i] < x)
|
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
fibM = fibMMm1;
|
2017-09-28 23:32:39 +03:00
|
|
|
fibMMm1 = fibMMm2;
|
|
|
|
fibMMm2 = fibM - fibMMm1;
|
|
|
|
offset = i;
|
|
|
|
}
|
2018-03-20 18:39:13 +03:00
|
|
|
|
2017-09-28 23:32:39 +03:00
|
|
|
/* If x is greater than the value at index fibMm2,
|
|
|
|
cut the subarray after i+1 */
|
|
|
|
else if (arr[i] > x)
|
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
fibM = fibMMm2;
|
2017-09-28 23:32:39 +03:00
|
|
|
fibMMm1 = fibMMm1 - fibMMm2;
|
|
|
|
fibMMm2 = fibM - fibMMm1;
|
|
|
|
}
|
2018-03-20 18:39:13 +03:00
|
|
|
|
2017-09-28 23:32:39 +03:00
|
|
|
/* element found. return index */
|
2020-05-29 23:23:24 +03:00
|
|
|
else
|
|
|
|
return i;
|
2017-09-28 23:32:39 +03:00
|
|
|
}
|
2018-03-20 18:39:13 +03:00
|
|
|
|
2017-09-28 23:32:39 +03:00
|
|
|
/* comparing the last element with x */
|
2020-05-29 23:23:24 +03:00
|
|
|
if (fibMMm1 && arr[offset + 1] == x)
|
|
|
|
return offset + 1;
|
2018-03-20 18:39:13 +03:00
|
|
|
|
2017-09-28 23:32:39 +03:00
|
|
|
/*element not found. return -1 */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
int arr[] = {10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100};
|
|
|
|
int n = sizeof(arr) / sizeof(arr[0]);
|
2017-09-28 23:32:39 +03:00
|
|
|
int x = 85;
|
2020-05-29 23:23:24 +03:00
|
|
|
printf("Found at index: %d", fibMonaccianSearch(arr, x, n));
|
2017-09-28 23:32:39 +03:00
|
|
|
return 0;
|
|
|
|
}
|