Skip to content

Commit 135b1ed

Browse files
committed
Fix
1 parent bd0af7c commit 135b1ed

File tree

1 file changed

+20
-41
lines changed

1 file changed

+20
-41
lines changed

src/main/java/com/thealgorithms/searches/FibonacciSearch.java

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,36 @@
22

33
import com.thealgorithms.devutils.searches.SearchAlgorithm;
44

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
88
*
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)
1712
*/
1813
public class FibonacciSearch implements SearchAlgorithm {
1914

2015
/**
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
2820
*/
2921
@Override
3022
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-
4123
int fibMinus1 = 1;
4224
int fibMinus2 = 0;
4325
int fibNumber = fibMinus1 + fibMinus2;
4426
int n = array.length;
45-
4627
while (fibNumber < n) {
4728
fibMinus2 = fibMinus1;
4829
fibMinus1 = fibNumber;
4930
fibNumber = fibMinus2 + fibMinus1;
5031
}
51-
5232
int offset = -1;
53-
5433
while (fibNumber > 1) {
5534
int i = Math.min(offset + fibMinus2, n - 1);
56-
5735
if (array[i].compareTo(key) < 0) {
5836
fibNumber = fibMinus1;
5937
fibMinus1 = fibMinus2;
@@ -67,20 +45,21 @@ public <T extends Comparable<T>> int find(T[] array, T key) {
6745
return i;
6846
}
6947
}
70-
7148
if (fibMinus1 == 1 && array[offset + 1] == key) {
7249
return offset + 1;
7350
}
74-
7551
return -1;
7652
}
7753

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);
8564
}
8665
}

0 commit comments

Comments
 (0)