|
| 1 | +/**************************************************************************** |
| 2 | + * Fibonacci Search JavaScript Implementation |
| 3 | + * Author Alhassan Atama Isiaka |
| 4 | + * Version v1.0.0 |
| 5 | + * Copyright 2020 |
| 6 | + * https://github.com/komputarist |
| 7 | + * |
| 8 | + * This implementation is based on Generalizing the Fibonacci search we |
| 9 | + * define the Fibonacci search of degree K. Like the Fibonacci search, |
| 10 | + * which it reduces to for K = 2, the Fibonacci search of degree K |
| 11 | + * involves only addition and subtraction. |
| 12 | + * Capocelli R.M. (1991) A Generalization of the Fibonacci Search. In: |
| 13 | + * Bergum G.E., Philippou A.N., Horadam A.F. (eds) Applications of Fibonacci |
| 14 | + * Numbers. Springer, Dordrecht. https://doi.org/10.1007/978-94-011-3586-3_9 |
| 15 | + * |
| 16 | + * This snippet is free. Feel free to improve on it |
| 17 | + * |
| 18 | + * We define a function fibonacciSearch() that takes an array of numbers, |
| 19 | + * the item (number) to be searched for and the length of the items in the array |
| 20 | + ****************************************************************************/ |
| 21 | + |
| 22 | +const fibonacciSearch = (arr, x, n) => { |
| 23 | + let fib2 = 0 // (K-2)'th Fibonacci Number |
| 24 | + let fib1 = 1 //(K-1)'th Fibonacci Number. |
| 25 | + let fibK = fib2 + fib1 //Kth Fibonacci |
| 26 | + |
| 27 | + //We want to store the smallest fibonacci number smaller such that |
| 28 | + //number is greater than or equal to n, we use fibK for this |
| 29 | + while (fibK < n) { |
| 30 | + fib2 = fib1 |
| 31 | + fib1 = fibK |
| 32 | + fibK = fib2 + fib1 |
| 33 | + } |
| 34 | + //This marks the eliminated range from front |
| 35 | + let offset = -1; |
| 36 | + |
| 37 | + |
| 38 | + /* while there are elements to be checked. We compare arr[fib2] with x. |
| 39 | + When fibM becomes 1, fib2 becomes 0 */ |
| 40 | + |
| 41 | + while (fibK > 1) { |
| 42 | + |
| 43 | + // Check if fibK is a valid location |
| 44 | + i = Math.min(offset + fib2, n - 1) |
| 45 | + |
| 46 | + /* If x is greater than the value at |
| 47 | + index fib2, Partition the subarray array |
| 48 | + from offset to i */ |
| 49 | + if (arr[i] < x) { |
| 50 | + fibK = fib1 |
| 51 | + fib1 = fib2 |
| 52 | + fib2 = fibK - fib1 |
| 53 | + offset = i |
| 54 | + // If x is greater than the value at |
| 55 | + // index fib2, cut the subarray array |
| 56 | + // from offset to i |
| 57 | + } else if (arr[i] > x) { |
| 58 | + fibK = fib2 |
| 59 | + fib1 = fib1 - fib2 |
| 60 | + fib2 = fibK - fib1 |
| 61 | + } |
| 62 | + //return index for found element |
| 63 | + else { |
| 64 | + return i |
| 65 | + } |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + //comparing the last element with x */ |
| 71 | + if (fib1 && arr[offset + 1] == x) { |
| 72 | + return offset + 1; |
| 73 | + } |
| 74 | + //element not found. return -1 |
| 75 | + return -1 |
| 76 | +} |
| 77 | +// Example |
| 78 | +arr = [10, 22, 35, 40, 45, 50, |
| 79 | + 80, 82, 85, 90, 100] |
| 80 | +n = arr.length |
| 81 | +x = 40 |
| 82 | +const fib = fibonacciSearch(arr, x, n) |
| 83 | +console.log("Element found at index:", fib) |
0 commit comments