Created
March 31, 2016 13:25
-
-
Save JohnRuddy/c73aa71ba5ad9a1bc37e6ab4303e13d7 to your computer and use it in GitHub Desktop.
The Fibonacci search uses the divide and conquer algorithm to sort the array, the Fibonacci search is an addition to the binary search in simple words it is an extension to the binary search.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This is a java program to search an element using Fibonacci search | |
import java.util.Scanner; | |
public class Fibonacci_Search | |
{ | |
static int kk = -1, nn = -1; | |
static int fib[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, | |
377, 610, 98, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, | |
75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, | |
3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, | |
102334155, 165580141 }; | |
static int fibsearch(int a[], int n, long x) | |
{ | |
int inf = 0, pos, k; | |
if (nn != n) | |
{ | |
k = 0; | |
while (fib[k] < n) k++; kk = k; nn = n; } else k = kk; while (k > 0) | |
{ | |
pos = inf + fib[--k]; | |
if ((pos >= n) || (x < a[pos])) ; else if (x > a[pos]) | |
{ | |
inf = pos + 1; | |
k--; | |
} | |
else | |
return pos; | |
} | |
return -1; | |
} | |
public static void main(String args[]) | |
{ | |
int arr[] = { 2, 3, 45, 56, 67, 78, 89, 99, 100, 101 }; | |
int num, pos; | |
Scanner scan = new Scanner(System.in); | |
System.out.println("Enter an element to search: "); | |
num = scan.nextInt(); | |
pos = fibsearch(arr, 10, num); | |
if (pos >= 0) | |
System.out.println("\nElement is at index : " | |
+ fibsearch(arr, 10, num)); | |
else | |
System.out.println("\nElement NOT found!! "); | |
scan.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source: http://codingsec.net/2016/03/the-faster-way-to-search-the-array-fibonacci-search/