Skip to content

Added the algorithm and test for Fibonacci Search #753

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 2 commits into from
May 13, 2019
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
69 changes: 69 additions & 0 deletions src/main/java/com/search/FibonacciSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package src.main.java.com.search;

import static java.lang.Math.min;

/**
* Fibonacci search is a method of searching a sorted array using a divide and conquer algorithm that narrows down
* possible locations with the aid of Fibonacci numbers. Compared to binary search where the sorted array is divided
* into two equal-sized parts, one of which is examined further, Fibonacci search divides the array into two parts that
* have sizes that are consecutive Fibonacci numbers.
* <p>
* Worst-case performance O(Log n)
* Best-case performance O(1)
* Average performance O(Log n)
* Average space complexity O(1)
*/
public class FibonacciSearch {
/**
* @param array is an array where the element should be found
* @param key is an element which should be found
* @param <T> is any comparable type
* @return The index position of the key in the array, returns -1 for empty array
*/
public <T extends Comparable<T>> int findIndex(T[] array, T key) {
int size = array.length;

if (size == 0)
return -1;

// Initialize the fibonacci numbers
int fibN1 = 1; // (n-1)th Fibonacci term
int fibN2 = 0; // (n-2)th Fibonacci term
int fibN = fibN1 + fibN2; // nth Fibonacci term

// fibN should store the smallest Fibonacci Number greater than or equal to size
while (fibN < size) {
fibN2 = fibN1;
fibN1 = fibN;
fibN = fibN2 + fibN1;
}

// Marks the eliminated range from front
int offset = -1;

while (fibN > 1) {
// Check if fibN2 is a valid location
int i = min(offset + fibN2, size - 1);

// If key is greater than the value at index fibN2, cuts the sub-array from offset to i
if (array[i].compareTo(key) < 0) {
fibN = fibN1;
fibN1 = fibN2;
fibN2 = fibN - fibN1;
offset = i;
}

// If x is greater than the value at index fibN2, cuts the sub-array after i+1
else if (array[i].compareTo(key) > 0) {
fibN = fibN2;
fibN1 = fibN1 - fibN2;
fibN2 = fibN - fibN1;
} else return i; // Element found
}
// comparing the last element with key
if (fibN1 == 1 && array[offset + 1].compareTo(key) == 0)
return offset + 1;

return -1; // Element not found
}
}
30 changes: 30 additions & 0 deletions src/test/java/com/search/FibonacciSearchTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package src.test.java.com.search;

import org.junit.Assert;
import org.junit.Test;
import src.main.java.com.search.FibonacciSearch;

public class FibonacciSearchTest {
@Test
public void testFibonacciSearch() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();

Integer[] arr = {11, 14, 23, 32, 36, 40, 54, 69};
int x = 54;
int index = fibonacciSearch.findIndex(arr, x);
Assert.assertEquals("Incorrect index", 6, index);

Integer[] arrTwo = {-400, -283, -180, -160, -129, -120, -30};
x = -120;
index = fibonacciSearch.findIndex(arrTwo, x);
Assert.assertEquals("Incorrect index", 5, index);

String[] arrString = {"101", "122", "136", "165", "225", "351", "458"};
String stringX = "136";
index = fibonacciSearch.findIndex(arrString, stringX);
Assert.assertEquals("Incorrect index", 2, index);

String[] arrThree = {};
Assert.assertEquals("Incorrect index", -1, fibonacciSearch.findIndex(arrThree, ""));
}
}