diff --git a/JavaPracticeHacktoberfest.iml b/JavaPracticeHacktoberfest.iml index 48426fcc..dac32f97 100644 --- a/JavaPracticeHacktoberfest.iml +++ b/JavaPracticeHacktoberfest.iml @@ -4,6 +4,7 @@ + \ No newline at end of file diff --git a/Searching/FibonacciSearch.java b/Searching/FibonacciSearch.java new file mode 100644 index 00000000..e8f01ec4 --- /dev/null +++ b/Searching/FibonacciSearch.java @@ -0,0 +1,94 @@ +/** + * Fibonacci Search Implementation in Java + * + * This class demonstrates the Fibonacci Search algorithm, an efficient + * searching technique for sorted arrays that uses Fibonacci numbers to + * divide the search space. The algorithm runs in O(log n) time. + * + * @author mtsagkl + */ +public class FibonacciSearch { + /** + * Executes the Fibonacci Search algorithm on a sorted array. + * This method uses Fibonacci numbers to split the search range . + * + * @param arr the sorted array in which the search is performed + * @param n the array's size + * @param key the element to search for + * @return the index of the key if found, otherwise -1 + */ + static int fibonacci_search(int arr[], int n,int key ){ + int offset =-1; + int fm2=0; + int fm1=1; + int fm=fm1+fm2; + + // find smallest Fibonacci number >= n + while(fm1){ + int i; + if (offset+fm2key){ + fm=fm2; + fm1=fm1-fm2; + fm2=fm-fm1; + } + // value found + else + return i; + } + // check last possible position + if (fm1==1 && arr[offset+1]==key) + return offset +1; + return -1; // not found + } + + /** + * Main method to demonstrate Fibonacci search functionality + */ + public static void main(String[] args) { + int n , key; + int arr[]={-15,-5,2,5,7,10,28,30,45,56}; + System.out.print("Array's elements: "); + for (int i=0; i= 0) + System.out.println("Found at index " + index); + else + System.out.println("Not found"); + + // Test with element not in array + key = 20; + System.out.println("The element to be searched: " + key); + index = fibonacci_search(arr, n, key); + if(index >= 0) + System.out.println("Found at index " + index); + else + System.out.println("Not found"); + } +}