Skip to content

Commit

Permalink
Added some header files and changed the min() function
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Bender committed Mar 20, 2018
1 parent b8cb524 commit 7668d1c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
Binary file added Searches/fibonacciSearch
Binary file not shown.
23 changes: 14 additions & 9 deletions Searches/fibonacciSearch.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include <stdio.h>
#include <stdlib.h>

int fibMonaccianSearch(int arr[], int x, int n)
{
/* Initialize fibonacci numbers */
int fibMMm2 = 0; // (m-2)'th Fibonacci No.
int fibMMm1 = 1; // (m-1)'th Fibonacci No.
int fibM = fibMMm2 + fibMMm1; // m'th Fibonacci

/* fibM is going to store the smallest Fibonacci
Number greater than or equal to n */
while (fibM < n)
Expand All @@ -13,18 +16,20 @@ int fibMonaccianSearch(int arr[], int x, int n)
fibMMm1 = fibM;
fibM = fibMMm2 + fibMMm1;
}

// Marks the eliminated range from front
int offset = -1;

/* 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
int i = min(offset+fibMMm2, n-1);


// sets i to the min. of (offset+fibMMm2) and (n-1)
int i = ((offset+fibMMm2) < (n-1)) ? (offset+fibMMm2) : (n-1);

/* If x is greater than the value at index fibMm2,
cut the subarray array from offset to i */
if (arr[i] < x)
Expand All @@ -34,7 +39,7 @@ int fibMonaccianSearch(int arr[], int x, int n)
fibMMm2 = fibM - fibMMm1;
offset = i;
}

/* If x is greater than the value at index fibMm2,
cut the subarray after i+1 */
else if (arr[i] > x)
Expand All @@ -43,14 +48,14 @@ int fibMonaccianSearch(int arr[], int x, int n)
fibMMm1 = fibMMm1 - fibMMm2;
fibMMm2 = fibM - fibMMm1;
}

/* element found. return index */
else return i;
}

/* comparing the last element with x */
if(fibMMm1 && arr[offset+1]==x)return offset+1;

/*element not found. return -1 */
return -1;
}
Expand Down
Binary file added Searches/fibonacciSearch.o
Binary file not shown.

0 comments on commit 7668d1c

Please sign in to comment.