From 18f8780467f1555471ec8eb08cdd67ff388f4bcd Mon Sep 17 00:00:00 2001 From: mtsagkl Date: Sun, 23 Nov 2025 22:51:08 +0200 Subject: [PATCH 1/3] Add Fibonacci Search implementation --- FibonacciSearch.java | 78 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 FibonacciSearch.java diff --git a/FibonacciSearch.java b/FibonacciSearch.java new file mode 100644 index 00000000..bb7d9680 --- /dev/null +++ b/FibonacciSearch.java @@ -0,0 +1,78 @@ +/** + * 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; + + while(fm1){ + int i; + if (offset+fm2key){ + fm=fm2; + fm1=fm1-fm2; + fm2=fm-fm1; + } + else + return i; + } + if (fm1==1 && arr[offset+1]==key) + return offset +1; + return -1; + } + + /** + * 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.print("\nFound at index " + index); + else + System.out.print("\nUnsuccessful search"); + } +} From f68d83c70d05c1c8ccbfeb2e56477931438aa1e7 Mon Sep 17 00:00:00 2001 From: mtsagkl Date: Sun, 23 Nov 2025 23:18:39 +0200 Subject: [PATCH 2/3] Add Fibonacci Search implementation --- FibonacciSearch.java | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/FibonacciSearch.java b/FibonacciSearch.java index bb7d9680..e8f01ec4 100644 --- a/FibonacciSearch.java +++ b/FibonacciSearch.java @@ -23,6 +23,7 @@ static int fibonacci_search(int arr[], int n,int key ){ int fm1=1; int fm=fm1+fm2; + // find smallest Fibonacci number >= n while(fmkey){ fm=fm2; fm1=fm1-fm2; fm2=fm-fm1; } - else + // value found + else return i; } + // check last possible position if (fm1==1 && arr[offset+1]==key) return offset +1; - return -1; + return -1; // not found } /** @@ -67,12 +72,23 @@ public static void main(String[] args) { System.out.print(arr[i]+" "); } n = arr.length; + + // Test with element in array key = 10; - System.out.print("\nThe element to be searched: " + key); + System.out.println("\nThe element to be searched: " + key); int index = fibonacci_search(arr, n, key); if(index >= 0) - System.out.print("\nFound at index " + index); + 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.print("\nUnsuccessful search"); + System.out.println("Not found"); } } From d8bb67a5b1446ca5b053e1149f28ad3ae6a0401d Mon Sep 17 00:00:00 2001 From: mtsagkl Date: Fri, 26 Dec 2025 19:14:21 +0200 Subject: [PATCH 3/3] Add Fibonacci Search implementation --- JavaPracticeHacktoberfest.iml | 1 + FibonacciSearch.java => Searching/FibonacciSearch.java | 0 2 files changed, 1 insertion(+) rename FibonacciSearch.java => Searching/FibonacciSearch.java (100%) 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/FibonacciSearch.java b/Searching/FibonacciSearch.java similarity index 100% rename from FibonacciSearch.java rename to Searching/FibonacciSearch.java