Skip to content

Add tests, remove main, improve docs in FibonacciSearch #5665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@
* [BM25InvertedIndexTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BM25InvertedIndexTest.java)
* [BreadthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BreadthFirstSearchTest.java)
* [DepthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/DepthFirstSearchTest.java)
* [FibonacciSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/FibonacciSearchTest.java)
* [HowManyTimesRotatedTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/HowManyTimesRotatedTest.java)
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
Expand Down
55 changes: 35 additions & 20 deletions src/main/java/com/thealgorithms/searches/FibonacciSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,42 @@

import com.thealgorithms.devutils.searches.SearchAlgorithm;

/*
* Fibonacci Search is a popular algorithm which finds the position of a target value in
* a sorted array
/**
* FibonacciSearch is a search algorithm that finds the position of a target value in
* a sorted array using Fibonacci numbers.
*
* The time complexity for this search algorithm is O(log3(n))
* The space complexity for this search algorithm is O(1)
* @author Kanakalatha Vemuru (https://github.com/KanakalathaVemuru)
* <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 {

/**
* @param array is a sorted array where the element has to be searched
* @param key is an element whose position has to be found
* @param <T> is any comparable type
* @return index of the element
* 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;
Expand Down Expand Up @@ -57,15 +75,12 @@ public <T extends Comparable<T>> int find(T[] array, T key) {
return -1;
}

// Driver Program
public static void main(String[] args) {
Integer[] integers = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};

int size = integers.length;
Integer targetValue = 128;
FibonacciSearch fsearch = new FibonacciSearch();
int atIndex = fsearch.find(integers, targetValue);

System.out.println("Should be found: " + targetValue + ". Found " + integers[atIndex] + " at index " + atIndex + ". An array length " + size);
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;
}
}
124 changes: 124 additions & 0 deletions src/test/java/com/thealgorithms/searches/FibonacciSearchTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.thealgorithms.searches;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;

/**
* Unit tests for the FibonacciSearch class.
*/
class FibonacciSearchTest {

/**
* Test for basic Fibonacci search functionality.
*/
@Test
void testFibonacciSearchFound() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
int key = 128;
int expectedIndex = 7; // Index of the key in the array
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the found element should be 7.");
}

/**
* Test for Fibonacci search when the element is not present.
*/
@Test
void testFibonacciSearchNotFound() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {1, 2, 4, 8, 16};
int key = 6; // Element not present in the array
int expectedIndex = -1; // Key not found
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The element should not be found in the array.");
}

/**
* Test for Fibonacci search with the first element as the key.
*/
@Test
void testFibonacciSearchFirstElement() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {1, 2, 4, 8, 16};
int key = 1; // First element
int expectedIndex = 0; // Index of the key in the array
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the first element should be 0.");
}

/**
* Test for Fibonacci search with the last element as the key.
*/
@Test
void testFibonacciSearchLastElement() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {1, 2, 4, 8, 16};
int key = 16; // Last element
int expectedIndex = 4; // Index of the key in the array
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the last element should be 4.");
}

/**
* Test for Fibonacci search with a single element present.
*/
@Test
void testFibonacciSearchSingleElementFound() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {1};
int key = 1; // Only element present
int expectedIndex = 0; // Index of the key in the array
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the single element should be 0.");
}

/**
* Test for Fibonacci search with a single element not present.
*/
@Test
void testFibonacciSearchSingleElementNotFound() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {1};
int key = 2; // Key not present
int expectedIndex = -1; // Key not found
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The element should not be found in the array.");
}

/**
* Test for Fibonacci search with an empty array.
*/
@Test
void testFibonacciSearchEmptyArray() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {}; // Empty array
int key = 1; // Key not present
assertThrows(IllegalArgumentException.class, () -> fibonacciSearch.find(array, key), "An empty array should throw an IllegalArgumentException.");
}

@Test
void testFibonacciSearchUnsortedArray() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {2, 1, 4, 3, 6, 5};
int key = 3; // Key not present
assertThrows(IllegalArgumentException.class, () -> fibonacciSearch.find(array, key), "An unsorted array should throw an IllegalArgumentException.");
}

@Test
void testFibonacciSearchNullKey() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {1, 2, 4, 8, 16};
Integer key = null; // Null key
assertThrows(IllegalArgumentException.class, () -> fibonacciSearch.find(array, key), "A null key should throw an IllegalArgumentException.");
}

/**
* Test for Fibonacci search on large array.
*/
@Test
void testFibonacciSearchLargeArray() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[] ::new); // Array from 0 to 9999
int key = 9999;
int expectedIndex = 9999;
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the last element should be 9999.");
}
}