2
2
3
3
import com .thealgorithms .devutils .searches .SearchAlgorithm ;
4
4
5
- /**
6
- * FibonacciSearch is a search algorithm that finds the position of a target value in
7
- * a sorted array using Fibonacci numbers.
5
+ /*
6
+ * Fibonacci Search is a popular algorithm which finds the position of a target value in
7
+ * a sorted array
8
8
*
9
- * <p>
10
- * The time complexity for this search algorithm is O(log n).
11
- * The space complexity for this search algorithm is O(1).
12
- * </p>
13
- *
14
- * <p>
15
- * Note: This algorithm requires that the input array be sorted.
16
- * </p>
9
+ * The time complexity for this search algorithm is O(log3(n))
10
+ * The space complexity for this search algorithm is O(1)
11
+ * @author Kanakalatha Vemuru (https://github.com/KanakalathaVemuru)
17
12
*/
18
13
public class FibonacciSearch implements SearchAlgorithm {
19
14
20
15
/**
21
- * Finds the index of the specified key in a sorted array using Fibonacci search.
22
- *
23
- * @param array The sorted array to search.
24
- * @param key The element to search for.
25
- * @param <T> The type of the elements in the array, which must be comparable.
26
- * @throws IllegalArgumentException if the input array is not sorted or empty, or if the key is null.
27
- * @return The index of the key if found, otherwise -1.
16
+ * @param array is a sorted array where the element has to be searched
17
+ * @param key is an element whose position has to be found
18
+ * @param <T> is any comparable type
19
+ * @return index of the element
28
20
*/
29
21
@ Override
30
22
public <T extends Comparable <T >> int find (T [] array , T key ) {
31
- if (array .length == 0 ) {
32
- throw new IllegalArgumentException ("Input array must not be empty." );
33
- }
34
- if (!isSorted (array )) {
35
- throw new IllegalArgumentException ("Input array must be sorted." );
36
- }
37
- if (key == null ) {
38
- throw new IllegalArgumentException ("Key must not be null." );
39
- }
40
-
41
23
int fibMinus1 = 1 ;
42
24
int fibMinus2 = 0 ;
43
25
int fibNumber = fibMinus1 + fibMinus2 ;
44
26
int n = array .length ;
45
-
46
27
while (fibNumber < n ) {
47
28
fibMinus2 = fibMinus1 ;
48
29
fibMinus1 = fibNumber ;
49
30
fibNumber = fibMinus2 + fibMinus1 ;
50
31
}
51
-
52
32
int offset = -1 ;
53
-
54
33
while (fibNumber > 1 ) {
55
34
int i = Math .min (offset + fibMinus2 , n - 1 );
56
-
57
35
if (array [i ].compareTo (key ) < 0 ) {
58
36
fibNumber = fibMinus1 ;
59
37
fibMinus1 = fibMinus2 ;
@@ -67,20 +45,21 @@ public <T extends Comparable<T>> int find(T[] array, T key) {
67
45
return i ;
68
46
}
69
47
}
70
-
71
48
if (fibMinus1 == 1 && array [offset + 1 ] == key ) {
72
49
return offset + 1 ;
73
50
}
74
-
75
51
return -1 ;
76
52
}
77
53
78
- private boolean isSorted (Comparable [] array ) {
79
- for (int i = 1 ; i < array .length ; i ++) {
80
- if (array [i - 1 ].compareTo (array [i ]) > 0 ) {
81
- return false ;
82
- }
83
- }
84
- return true ;
54
+ // Driver Program
55
+ public static void main (String [] args ) {
56
+ Integer [] integers = {1 , 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 , 512 };
57
+
58
+ int size = integers .length ;
59
+ Integer targetValue = 128 ;
60
+ FibonacciSearch fsearch = new FibonacciSearch ();
61
+ int atIndex = fsearch .find (integers , targetValue );
62
+
63
+ System .out .println ("Should be found: " + targetValue + ". Found " + integers [atIndex ] + " at index " + atIndex + ". An array length " + size );
85
64
}
86
65
}
0 commit comments