|
| 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 | + /* while there are elements to be checked. We compare arr[fib2] with x. |
| 38 | + When fibM becomes 1, fib2 becomes 0 */ |
| 39 | + |
| 40 | + while (fibK > 1) { |
| 41 | + // Check if fibK is a valid location |
| 42 | + const i = Math.min(offset + fib2, n - 1) |
| 43 | + |
| 44 | + /* If x is greater than the value at |
| 45 | + index fib2, Partition the subarray array |
| 46 | + from offset to i */ |
| 47 | + if (arr[i] < x) { |
| 48 | + fibK = fib1 |
| 49 | + fib1 = fib2 |
| 50 | + fib2 = fibK - fib1 |
| 51 | + offset = i |
| 52 | + /* If x is greater than the value at |
| 53 | + index fib2, cut the subarray array |
| 54 | + from offset to i */ |
| 55 | + } else if (arr[i] > x) { |
| 56 | + fibK = fib2 |
| 57 | + fib1 = fib1 - fib2 |
| 58 | + fib2 = fibK - fib1 |
| 59 | + } else { |
| 60 | + // return index for found element |
| 61 | + return i |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // comparing the last element with x */ |
| 66 | + if (fib1 && arr[offset + 1] === x) { |
| 67 | + return offset + 1 |
| 68 | + } |
| 69 | + // element not found. return -1 |
| 70 | + return -1 |
| 71 | +} |
| 72 | +// Example |
| 73 | +const myArray = [10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100] |
| 74 | +const n = myArray.length |
| 75 | +const x = 90 |
| 76 | +const fibFinder = fibonacciSearch(myArray, x, n) |
| 77 | +console.log('Element found at index:', fibFinder) |
0 commit comments