MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / Fibonacci

Class Fibonacci

Misc/Fibonacci_Search/solution.java:25–103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

23import java.util.*;
24
25class Fibonacci
26{
27 // Utility function to find minimum
28 // of two elements
29 public static int min(int x, int y)
30 { return (x <= y)? x : y; }
31
32 /* Returns index of x if present, else returns -1 */
33 public static int fibMonaccianSearch(int arr[],
34 int x, int n)
35 {
36 /* Initialize fibonacci numbers */
37 int fibMMm2 = 0; // (m-2)'th Fibonacci No.
38 int fibMMm1 = 1; // (m-1)'th Fibonacci No.
39 int fibM = fibMMm2 + fibMMm1; // m'th Fibonacci
40
41 /* fibM is going to store the smallest
42 Fibonacci Number greater than or equal to n */
43 while (fibM < n)
44 {
45 fibMMm2 = fibMMm1;
46 fibMMm1 = fibM;
47 fibM = fibMMm2 + fibMMm1;
48 }
49
50 // Marks the eliminated range from front
51 int offset = -1;
52
53 /* while there are elements to be inspected.
54 Note that we compare arr[fibMm2] with x.
55 When fibM becomes 1, fibMm2 becomes 0 */
56 while (fibM > 1)
57 {
58 // Check if fibMm2 is a valid location
59 int i = min(offset+fibMMm2, n-1);
60
61 /* If x is greater than the value at
62 index fibMm2, cut the subarray array
63 from offset to i */
64 if (arr[i] < x)
65 {
66 fibM = fibMMm1;
67 fibMMm1 = fibMMm2;
68 fibMMm2 = fibM - fibMMm1;
69 offset = i;
70 }
71
72 /* If x is less than the value at index
73 fibMm2, cut the subarray after i+1 */
74 else if (arr[i] > x)
75 {
76 fibM = fibMMm2;
77 fibMMm1 = fibMMm1 - fibMMm2;
78 fibMMm2 = fibM - fibMMm1;
79 }
80
81 /* element found. return index */
82 else return i;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected