From 36f977936db733e237eab6968d7016890350fefa Mon Sep 17 00:00:00 2001 From: Abhijay Kumar Date: Mon, 13 May 2019 16:36:38 +0530 Subject: [PATCH 1/2] Added the algorithm and test for Fibonacci Search --- src/main/java/com/search/FibonacciSearch.java | 69 +++++++++++++++++++ .../java/com/search/FibonacciSearchTest.java | 30 ++++++++ 2 files changed, 99 insertions(+) create mode 100644 src/main/java/com/search/FibonacciSearch.java create mode 100644 src/test/java/com/search/FibonacciSearchTest.java diff --git a/src/main/java/com/search/FibonacciSearch.java b/src/main/java/com/search/FibonacciSearch.java new file mode 100644 index 000000000000..3a3a4c548f26 --- /dev/null +++ b/src/main/java/com/search/FibonacciSearch.java @@ -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. + *

+ * 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 is any comparable type + * @return The index position of the key in the array, returns -1 for empty array + */ + public > 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 + } +} diff --git a/src/test/java/com/search/FibonacciSearchTest.java b/src/test/java/com/search/FibonacciSearchTest.java new file mode 100644 index 000000000000..2adae9d72b64 --- /dev/null +++ b/src/test/java/com/search/FibonacciSearchTest.java @@ -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, "")); + } +} From 09c4cd790fea45e823d859e4049449d82b71e2f4 Mon Sep 17 00:00:00 2001 From: Libin Yang Date: Mon, 13 May 2019 21:29:00 +0800 Subject: [PATCH 2/2] Update FibonacciSearch.java Add single space after `//` --- src/main/java/com/search/FibonacciSearch.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/search/FibonacciSearch.java b/src/main/java/com/search/FibonacciSearch.java index 3a3a4c548f26..3bbcd0f89a7e 100644 --- a/src/main/java/com/search/FibonacciSearch.java +++ b/src/main/java/com/search/FibonacciSearch.java @@ -31,7 +31,7 @@ public > int findIndex(T[] array, T key) { 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 + // fibN should store the smallest Fibonacci Number greater than or equal to size while (fibN < size) { fibN2 = fibN1; fibN1 = fibN; @@ -45,7 +45,7 @@ public > int findIndex(T[] array, T key) { // 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 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; @@ -53,12 +53,12 @@ public > int findIndex(T[] array, T key) { offset = i; } - //If x is greater than the value at index fibN2, cuts the sub-array after i+1 + // 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 + } else return i; // Element found } // comparing the last element with key if (fibN1 == 1 && array[offset + 1].compareTo(key) == 0)