Merge pull request #753 from abhijay94/Development
Added the algorithm and test for Fibonacci Search
This commit is contained in:
commit
e2a6b024b1
69
src/main/java/com/search/FibonacciSearch.java
Normal file
69
src/main/java/com/search/FibonacciSearch.java
Normal file
@ -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
src/test/java/com/search/FibonacciSearchTest.java
Normal file
30
src/test/java/com/search/FibonacciSearchTest.java
Normal file
@ -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, ""));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user