-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathFibonacciSearch.java
86 lines (75 loc) · 2.59 KB
/
FibonacciSearch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.thealgorithms.searches;
import com.thealgorithms.devutils.searches.SearchAlgorithm;
/**
* FibonacciSearch is a search algorithm that finds the position of a target value in
* a sorted array using Fibonacci numbers.
*
* <p>
* The time complexity for this search algorithm is O(log n).
* The space complexity for this search algorithm is O(1).
* </p>
*
* <p>
* Note: This algorithm requires that the input array be sorted.
* </p>
*/
public class FibonacciSearch implements SearchAlgorithm {
/**
* Finds the index of the specified key in a sorted array using Fibonacci search.
*
* @param array The sorted array to search.
* @param key The element to search for.
* @param <T> The type of the elements in the array, which must be comparable.
* @throws IllegalArgumentException if the input array is not sorted or empty, or if the key is null.
* @return The index of the key if found, otherwise -1.
*/
@Override
public <T extends Comparable<T>> int find(T[] array, T key) {
if (array.length == 0) {
throw new IllegalArgumentException("Input array must not be empty.");
}
if (!isSorted(array)) {
throw new IllegalArgumentException("Input array must be sorted.");
}
if (key == null) {
throw new IllegalArgumentException("Key must not be null.");
}
int fibMinus1 = 1;
int fibMinus2 = 0;
int fibNumber = fibMinus1 + fibMinus2;
int n = array.length;
while (fibNumber < n) {
fibMinus2 = fibMinus1;
fibMinus1 = fibNumber;
fibNumber = fibMinus2 + fibMinus1;
}
int offset = -1;
while (fibNumber > 1) {
int i = Math.min(offset + fibMinus2, n - 1);
if (array[i].compareTo(key) < 0) {
fibNumber = fibMinus1;
fibMinus1 = fibMinus2;
fibMinus2 = fibNumber - fibMinus1;
offset = i;
} else if (array[i].compareTo(key) > 0) {
fibNumber = fibMinus2;
fibMinus1 = fibMinus1 - fibMinus2;
fibMinus2 = fibNumber - fibMinus1;
} else {
return i;
}
}
if (fibMinus1 == 1 && array[offset + 1] == key) {
return offset + 1;
}
return -1;
}
private boolean isSorted(Comparable[] array) {
for (int i = 1; i < array.length; i++) {
if (array[i - 1].compareTo(array[i]) > 0) {
return false;
}
}
return true;
}
}