mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
88 lines
2.6 KiB
Java
88 lines
2.6 KiB
Java
package com.thealgorithms.searches;
|
|
|
|
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
|
|
|
/**
|
|
* FibonacciSearch is a search algorithm that finds the position of a target value in
|
|
* a sorted array using Fibonacci numbers.
|
|
*
|
|
* <p>
|
|
* The time complexity for this search algorithm is O(log n).
|
|
* The space complexity for this search algorithm is O(1).
|
|
* </p>
|
|
*
|
|
* <p>
|
|
* Note: This algorithm requires that the input array be sorted.
|
|
* </p>
|
|
*/
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
public class FibonacciSearch implements SearchAlgorithm {
|
|
|
|
/**
|
|
* Finds the index of the specified key in a sorted array using Fibonacci search.
|
|
*
|
|
* @param array The sorted array to search.
|
|
* @param key The element to search for.
|
|
* @param <T> The type of the elements in the array, which must be comparable.
|
|
* @throws IllegalArgumentException if the input array is not sorted or empty, or if the key is null.
|
|
* @return The index of the key if found, otherwise -1.
|
|
*/
|
|
@Override
|
|
public <T extends Comparable<T>> int find(T[] array, T key) {
|
|
if (array.length == 0) {
|
|
throw new IllegalArgumentException("Input array must not be empty.");
|
|
}
|
|
if (!isSorted(array)) {
|
|
throw new IllegalArgumentException("Input array must be sorted.");
|
|
}
|
|
if (key == null) {
|
|
throw new IllegalArgumentException("Key must not be null.");
|
|
}
|
|
|
|
int fibMinus1 = 1;
|
|
int fibMinus2 = 0;
|
|
int fibNumber = fibMinus1 + fibMinus2;
|
|
int n = array.length;
|
|
|
|
while (fibNumber < n) {
|
|
fibMinus2 = fibMinus1;
|
|
fibMinus1 = fibNumber;
|
|
fibNumber = fibMinus2 + fibMinus1;
|
|
}
|
|
|
|
int offset = -1;
|
|
|
|
while (fibNumber > 1) {
|
|
int i = Math.min(offset + fibMinus2, n - 1);
|
|
|
|
if (array[i].compareTo(key) < 0) {
|
|
fibNumber = fibMinus1;
|
|
fibMinus1 = fibMinus2;
|
|
fibMinus2 = fibNumber - fibMinus1;
|
|
offset = i;
|
|
} else if (array[i].compareTo(key) > 0) {
|
|
fibNumber = fibMinus2;
|
|
fibMinus1 = fibMinus1 - fibMinus2;
|
|
fibMinus2 = fibNumber - fibMinus1;
|
|
} else {
|
|
return i;
|
|
}
|
|
}
|
|
|
|
if (fibMinus1 == 1 && array[offset + 1] == key) {
|
|
return offset + 1;
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
private boolean isSorted(Comparable[] array) {
|
|
for (int i = 1; i < array.length; i++) {
|
|
if (array[i - 1].compareTo(array[i]) > 0) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|