-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathFibonacci_search.java
86 lines (75 loc) · 2.84 KB
/
Fibonacci_search.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Find a Fibonacci number that is greater than or equal to the size of the array in which we are searching for the key.
// Compare the key with the predecessor of the Fibonacci number obtained in Step 1 and store it in index.
// If the key and array element at index are equal then the key is present at position index + 1.
// If the key is less than array element at index, then search the left sub-tree to index.
// If the given key is greater than the array element at index, then search the right sub-tree to index.
// If the key is not found, repeat the steps from Step 1 to Step 5 as long as index = 0, that is, Fibonacci number >= array_size.
// After each iteration the size of array array_size is reduced.
package Java;
import java.util.*;
public class FibonacciSearch {
public static int getFibanociNumber(int array_Size) {
int fibk = 0;
int fibk2 = 0;
int fibk1 = 1;
if (array_Size == 0 || array_Size == 1) {
return 0;
}
while (fibk < array_Size) {
fibk = fibk1 + fibk2;
fibk2 = fibk1;
fibk1 = fibk;
}
return fibk2;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int sorted_Array[] = new int[100];
int key, index, low, high, flag, location, array_Size;
System.out.println("Enter the size of the array: ");
array_Size = s.nextInt();
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < array_Size; i++) {
sorted_Array[i] = s.nextInt();
}
low = 0;
location = -1;
high = array_Size - 1;
flag = 0;
System.out.println("Enter the element which is to be searched: ");
key = s.nextInt();
s.close();
index = 0;
while (flag != 1 && low <= high) {
index = getFibanociNumber(array_Size);
if (key == sorted_Array[index + low]) {
location = low + index;
flag = 1;
break;
}
else if (key > sorted_Array[index + low]){
low = low + index + 1;
}
else {
high = low + index - 1;
}
array_Size = high - low + 1;
}
if (flag == 1){
location += 1;
System.out.println("The given element " + key + " found at location " + location + " ");
}
else {
System.out.println("The given element " + key + " is not present in the given array");
}
}
}
/*TEST CASES
Enter the size of the array: 10
Enter the elements of the array: -15 0 9 14 23 35 49 52 65 77
Enter the element which is to be searched: 52
The given element 52 found at location 8
COMPLEXITY
Time complexity O(log n)
Space Complexity O(1)
*/