Skip to content

Commit ab6e4af

Browse files
HardvanChiefpatwal
authored andcommitted
Add tests, remove main, improve docs in FibonacciSearch (TheAlgorithms#5665)
1 parent 9fd634a commit ab6e4af

File tree

3 files changed

+160
-20
lines changed

3 files changed

+160
-20
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,7 @@
10011001
* [BM25InvertedIndexTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BM25InvertedIndexTest.java)
10021002
* [BreadthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BreadthFirstSearchTest.java)
10031003
* [DepthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/DepthFirstSearchTest.java)
1004+
* [FibonacciSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/FibonacciSearchTest.java)
10041005
* [HowManyTimesRotatedTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/HowManyTimesRotatedTest.java)
10051006
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)
10061007
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)

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

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,42 @@
22

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

5-
/*
6-
* Fibonacci Search is a popular algorithm which finds the position of a target value in
7-
* a sorted array
5+
/**
6+
* FibonacciSearch is a search algorithm that finds the position of a target value in
7+
* a sorted array using Fibonacci numbers.
88
*
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)
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>
1217
*/
1318
public class FibonacciSearch implements SearchAlgorithm {
1419

1520
/**
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
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.
2028
*/
2129
@Override
2230
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+
2341
int fibMinus1 = 1;
2442
int fibMinus2 = 0;
2543
int fibNumber = fibMinus1 + fibMinus2;
@@ -57,15 +75,12 @@ public <T extends Comparable<T>> int find(T[] array, T key) {
5775
return -1;
5876
}
5977

60-
// Driver Program
61-
public static void main(String[] args) {
62-
Integer[] integers = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
63-
64-
int size = integers.length;
65-
Integer targetValue = 128;
66-
FibonacciSearch fsearch = new FibonacciSearch();
67-
int atIndex = fsearch.find(integers, targetValue);
68-
69-
System.out.println("Should be found: " + targetValue + ". Found " + integers[atIndex] + " at index " + atIndex + ". An array length " + size);
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;
7085
}
7186
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.stream.IntStream;
7+
import org.junit.jupiter.api.Test;
8+
9+
/**
10+
* Unit tests for the FibonacciSearch class.
11+
*/
12+
class FibonacciSearchTest {
13+
14+
/**
15+
* Test for basic Fibonacci search functionality.
16+
*/
17+
@Test
18+
void testFibonacciSearchFound() {
19+
FibonacciSearch fibonacciSearch = new FibonacciSearch();
20+
Integer[] array = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
21+
int key = 128;
22+
int expectedIndex = 7; // Index of the key in the array
23+
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the found element should be 7.");
24+
}
25+
26+
/**
27+
* Test for Fibonacci search when the element is not present.
28+
*/
29+
@Test
30+
void testFibonacciSearchNotFound() {
31+
FibonacciSearch fibonacciSearch = new FibonacciSearch();
32+
Integer[] array = {1, 2, 4, 8, 16};
33+
int key = 6; // Element not present in the array
34+
int expectedIndex = -1; // Key not found
35+
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The element should not be found in the array.");
36+
}
37+
38+
/**
39+
* Test for Fibonacci search with the first element as the key.
40+
*/
41+
@Test
42+
void testFibonacciSearchFirstElement() {
43+
FibonacciSearch fibonacciSearch = new FibonacciSearch();
44+
Integer[] array = {1, 2, 4, 8, 16};
45+
int key = 1; // First element
46+
int expectedIndex = 0; // Index of the key in the array
47+
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the first element should be 0.");
48+
}
49+
50+
/**
51+
* Test for Fibonacci search with the last element as the key.
52+
*/
53+
@Test
54+
void testFibonacciSearchLastElement() {
55+
FibonacciSearch fibonacciSearch = new FibonacciSearch();
56+
Integer[] array = {1, 2, 4, 8, 16};
57+
int key = 16; // Last element
58+
int expectedIndex = 4; // Index of the key in the array
59+
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the last element should be 4.");
60+
}
61+
62+
/**
63+
* Test for Fibonacci search with a single element present.
64+
*/
65+
@Test
66+
void testFibonacciSearchSingleElementFound() {
67+
FibonacciSearch fibonacciSearch = new FibonacciSearch();
68+
Integer[] array = {1};
69+
int key = 1; // Only element present
70+
int expectedIndex = 0; // Index of the key in the array
71+
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the single element should be 0.");
72+
}
73+
74+
/**
75+
* Test for Fibonacci search with a single element not present.
76+
*/
77+
@Test
78+
void testFibonacciSearchSingleElementNotFound() {
79+
FibonacciSearch fibonacciSearch = new FibonacciSearch();
80+
Integer[] array = {1};
81+
int key = 2; // Key not present
82+
int expectedIndex = -1; // Key not found
83+
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The element should not be found in the array.");
84+
}
85+
86+
/**
87+
* Test for Fibonacci search with an empty array.
88+
*/
89+
@Test
90+
void testFibonacciSearchEmptyArray() {
91+
FibonacciSearch fibonacciSearch = new FibonacciSearch();
92+
Integer[] array = {}; // Empty array
93+
int key = 1; // Key not present
94+
assertThrows(IllegalArgumentException.class, () -> fibonacciSearch.find(array, key), "An empty array should throw an IllegalArgumentException.");
95+
}
96+
97+
@Test
98+
void testFibonacciSearchUnsortedArray() {
99+
FibonacciSearch fibonacciSearch = new FibonacciSearch();
100+
Integer[] array = {2, 1, 4, 3, 6, 5};
101+
int key = 3; // Key not present
102+
assertThrows(IllegalArgumentException.class, () -> fibonacciSearch.find(array, key), "An unsorted array should throw an IllegalArgumentException.");
103+
}
104+
105+
@Test
106+
void testFibonacciSearchNullKey() {
107+
FibonacciSearch fibonacciSearch = new FibonacciSearch();
108+
Integer[] array = {1, 2, 4, 8, 16};
109+
Integer key = null; // Null key
110+
assertThrows(IllegalArgumentException.class, () -> fibonacciSearch.find(array, key), "A null key should throw an IllegalArgumentException.");
111+
}
112+
113+
/**
114+
* Test for Fibonacci search on large array.
115+
*/
116+
@Test
117+
void testFibonacciSearchLargeArray() {
118+
FibonacciSearch fibonacciSearch = new FibonacciSearch();
119+
Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[] ::new); // Array from 0 to 9999
120+
int key = 9999;
121+
int expectedIndex = 9999;
122+
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the last element should be 9999.");
123+
}
124+
}

0 commit comments

Comments
 (0)