forked from deutranium/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibonacciSearch.cs
50 lines (44 loc) · 1.45 KB
/
FibonacciSearch.cs
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
using System;
class MainClass {
public static void Main (string[] args) {
Console.WriteLine("How big will be the array?");
int arrayLength = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
int [] array = new int[arrayLength];
int i;
for (i=0; i<arrayLength; i++) {
array[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("\nWhat's the number you are looking for?");
int searchedNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine($"Number found at index: {FibonacciSearch(array, searchedNumber, arrayLength)}.");
}
public static int RecursiveFibonacci(int n) {
if (n == 1) {
return 0;
} else if ( n == 2 ) {
return 1;
} else {
return RecursiveFibonacci(n-1) + RecursiveFibonacci(n-2);
}
}
public static int FibonacciSearch(int []array, int searchedNumber, int arrayLength) {
int counter = 1;
while (RecursiveFibonacci(counter) < arrayLength) {
counter++;
}
int offset = -1;
while (RecursiveFibonacci(counter) > 1) {
int index = Math.Min(offset+RecursiveFibonacci(counter-2), arrayLength-1);
if (array[index] < searchedNumber) {
counter--;
offset = index;
} else if (array[index] > searchedNumber) {
counter -= 2;
} else return index;
}
if (RecursiveFibonacci(counter-1) == 1 && array[offset-1] == searchedNumber) {
return offset + 1;
} else return -1;
}
}