From f436149d9efee4fee61312983eb364f554c75d54 Mon Sep 17 00:00:00 2001 From: Alhassan Atama Date: Fri, 2 Oct 2020 07:32:46 +0100 Subject: [PATCH 1/4] Implemented FibonacciSearch Using JavaScript --- Search/FibonacciSearch.js | 83 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 Search/FibonacciSearch.js diff --git a/Search/FibonacciSearch.js b/Search/FibonacciSearch.js new file mode 100644 index 0000000000..8d315608a2 --- /dev/null +++ b/Search/FibonacciSearch.js @@ -0,0 +1,83 @@ +/**************************************************************************** + * Fibonacci Search JavaScript Implementation + * Author Alhassan Atama Isiaka + * Version v1.0.0 + * Copyright 2020 + * https://github.com/komputarist + * + * This implementation is based on Generalizing the Fibonacci search we + * define the Fibonacci search of degree K. Like the Fibonacci search, + * which it reduces to for K = 2, the Fibonacci search of degree K + * involves only addition and subtraction. + * Capocelli R.M. (1991) A Generalization of the Fibonacci Search. In: + * Bergum G.E., Philippou A.N., Horadam A.F. (eds) Applications of Fibonacci + * Numbers. Springer, Dordrecht. https://doi.org/10.1007/978-94-011-3586-3_9 + * + * This snippet is free. Feel free to improve on it + * + * We define a function fibonacciSearch() that takes an array of numbers, + * the item (number) to be searched for and the length of the items in the array + ****************************************************************************/ + +const fibonacciSearch = (arr, x, n) => { + let fib2 = 0 // (K-2)'th Fibonacci Number + let fib1 = 1 //(K-1)'th Fibonacci Number. + let fibK = fib2 + fib1 //Kth Fibonacci + + //We want to store the smallest fibonacci number smaller such that + //number is greater than or equal to n, we use fibK for this + while (fibK < n) { + fib2 = fib1 + fib1 = fibK + fibK = fib2 + fib1 + } + //This marks the eliminated range from front + let offset = -1; + + + /* while there are elements to be checked. We compare arr[fib2] with x. + When fibM becomes 1, fib2 becomes 0 */ + + while (fibK > 1) { + + // Check if fibK is a valid location + i = Math.min(offset + fib2, n - 1) + + /* If x is greater than the value at + index fib2, Partition the subarray array + from offset to i */ + if (arr[i] < x) { + fibK = fib1 + fib1 = fib2 + fib2 = fibK - fib1 + offset = i + // If x is greater than the value at + // index fib2, cut the subarray array + // from offset to i + } else if (arr[i] > x) { + fibK = fib2 + fib1 = fib1 - fib2 + fib2 = fibK - fib1 + } + //return index for found element + else { + return i + } + + } + + + //comparing the last element with x */ + if (fib1 && arr[offset + 1] == x) { + return offset + 1; + } + //element not found. return -1 + return -1 +} +// Example +arr = [10, 22, 35, 40, 45, 50, + 80, 82, 85, 90, 100] +n = arr.length +x = 40 +const fib = fibonacciSearch(arr, x, n) +console.log("Element found at index:", fib) From 351f7ea47d60b96331c9cc371b93f31ac5454c4b Mon Sep 17 00:00:00 2001 From: Alhassan Atama Date: Fri, 2 Oct 2020 10:14:34 +0100 Subject: [PATCH 2/4] Fixed Lint/test issues --- Search/FibonacciSearch.js | 62 +++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/Search/FibonacciSearch.js b/Search/FibonacciSearch.js index 8d315608a2..637b649031 100644 --- a/Search/FibonacciSearch.js +++ b/Search/FibonacciSearch.js @@ -2,12 +2,12 @@ * Fibonacci Search JavaScript Implementation * Author Alhassan Atama Isiaka * Version v1.0.0 - * Copyright 2020 + * Copyright 2020 * https://github.com/komputarist * * This implementation is based on Generalizing the Fibonacci search we * define the Fibonacci search of degree K. Like the Fibonacci search, - * which it reduces to for K = 2, the Fibonacci search of degree K + * which it reduces to for K = 2, the Fibonacci search of degree K * involves only addition and subtraction. * Capocelli R.M. (1991) A Generalization of the Fibonacci Search. In: * Bergum G.E., Philippou A.N., Horadam A.F. (eds) Applications of Fibonacci @@ -20,64 +20,62 @@ ****************************************************************************/ const fibonacciSearch = (arr, x, n) => { - let fib2 = 0 // (K-2)'th Fibonacci Number - let fib1 = 1 //(K-1)'th Fibonacci Number. - let fibK = fib2 + fib1 //Kth Fibonacci + let fib2 = 0 // (K-2)'th Fibonacci Number + let fib1 = 1 // (K-1)'th Fibonacci Number. + let fibK = fib2 + fib1 //Kth Fibonacci - //We want to store the smallest fibonacci number smaller such that - //number is greater than or equal to n, we use fibK for this + /* We want to store the smallest fibonacci number smaller such that + number is greater than or equal to n, we use fibK for this */ while (fibK < n) { fib2 = fib1 fib1 = fibK fibK = fib2 + fib1 } - //This marks the eliminated range from front - let offset = -1; + // This marks the eliminated range from front + let offset = -1 - - /* while there are elements to be checked. We compare arr[fib2] with x. + /* while there are elements to be checked. We compare arr[fib2] with x. When fibM becomes 1, fib2 becomes 0 */ while (fibK > 1) { + // Check if fibK is a valid location + let i = Math.min(offset + fib2, n - 1) - // Check if fibK is a valid location - i = Math.min(offset + fib2, n - 1) - - /* If x is greater than the value at - index fib2, Partition the subarray array + /* If x is greater than the value at + index fib2, Partition the subarray array from offset to i */ if (arr[i] < x) { fibK = fib1 fib1 = fib2 fib2 = fibK - fib1 offset = i - // If x is greater than the value at - // index fib2, cut the subarray array - // from offset to i - } else if (arr[i] > x) { + /* If x is greater than the value at + index fib2, cut the subarray array + from offset to i */ + } + else if (arr[i] > x) { fibK = fib2 fib1 = fib1 - fib2 fib2 = fibK - fib1 } - //return index for found element + // return index for found element else { return i } } - - //comparing the last element with x */ - if (fib1 && arr[offset + 1] == x) { - return offset + 1; + //comparing the last element with x */ + if (fib1 && arr[offset + 1] === x) { + return offset + 1 } - //element not found. return -1 + //element not found. return -1 return -1 } -// Example -arr = [10, 22, 35, 40, 45, 50, +// Example +const myArray = [10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100] -n = arr.length -x = 40 -const fib = fibonacciSearch(arr, x, n) -console.log("Element found at index:", fib) +let n = myArray.length +let x = 90 +const fibFinder = fibonacciSearch(myArray, x, n) +console.log("Element found at index:", fibFinder) \ No newline at end of file From 15ad2a0ac3356bb4136727edd1acaa77494a8e15 Mon Sep 17 00:00:00 2001 From: Alhassan Atama Date: Fri, 2 Oct 2020 10:41:13 +0100 Subject: [PATCH 3/4] Fixed lint/test error --- Search/FibonacciSearch.js | 102 +++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 52 deletions(-) diff --git a/Search/FibonacciSearch.js b/Search/FibonacciSearch.js index 637b649031..576bff0e81 100644 --- a/Search/FibonacciSearch.js +++ b/Search/FibonacciSearch.js @@ -12,70 +12,68 @@ * Capocelli R.M. (1991) A Generalization of the Fibonacci Search. In: * Bergum G.E., Philippou A.N., Horadam A.F. (eds) Applications of Fibonacci * Numbers. Springer, Dordrecht. https://doi.org/10.1007/978-94-011-3586-3_9 - * + * * This snippet is free. Feel free to improve on it - * + * * We define a function fibonacciSearch() that takes an array of numbers, * the item (number) to be searched for and the length of the items in the array ****************************************************************************/ const fibonacciSearch = (arr, x, n) => { - let fib2 = 0 // (K-2)'th Fibonacci Number - let fib1 = 1 // (K-1)'th Fibonacci Number. - let fibK = fib2 + fib1 //Kth Fibonacci - - /* We want to store the smallest fibonacci number smaller such that - number is greater than or equal to n, we use fibK for this */ - while (fibK < n) { - fib2 = fib1 - fib1 = fibK - fibK = fib2 + fib1 - } - // This marks the eliminated range from front - let offset = -1 + let fib2 = 0 // (K-2)'th Fibonacci Number + let fib1 = 1 // (K-1)'th Fibonacci Number. + let fibK = fib2 + fib1 //Kth Fibonacci - /* while there are elements to be checked. We compare arr[fib2] with x. - When fibM becomes 1, fib2 becomes 0 */ + /* We want to store the smallest fibonacci number smaller such that + number is greater than or equal to n, we use fibK for this */ + while (fibK < n) { + fib2 = fib1 + fib1 = fibK + fibK = fib2 + fib1 + } + // This marks the eliminated range from front + let offset = -1 - while (fibK > 1) { - // Check if fibK is a valid location - let i = Math.min(offset + fib2, n - 1) + /* while there are elements to be checked. We compare arr[fib2] with x. + When fibM becomes 1, fib2 becomes 0 */ - /* If x is greater than the value at - index fib2, Partition the subarray array - from offset to i */ - if (arr[i] < x) { - fibK = fib1 - fib1 = fib2 - fib2 = fibK - fib1 - offset = i - /* If x is greater than the value at - index fib2, cut the subarray array - from offset to i */ - } - else if (arr[i] > x) { - fibK = fib2 - fib1 = fib1 - fib2 - fib2 = fibK - fib1 - } - // return index for found element - else { - return i - } + while (fibK > 1) { + // Check if fibK is a valid location + const i = Math.min(offset + fib2, n - 1) + /* If x is greater than the value at + index fib2, Partition the subarray array + from offset to i */ + if (arr[i] < x) { + fibK = fib1 + fib1 = fib2 + fib2 = fibK - fib1 + offset = i + /* If x is greater than the value at + index fib2, cut the subarray array + from offset to i */ } - - //comparing the last element with x */ - if (fib1 && arr[offset + 1] === x) { - return offset + 1 + else if (arr[i] > x) { + fibK = fib2 + fib1 = fib1 - fib2 + fib2 = fibK - fib1 + } + // return index for found element + else { + return i } - //element not found. return -1 - return -1 +} + + // comparing the last element with x */ + if (fib1 && arr[offset + 1] === x) { + return offset + 1 + } + // element not found. return -1 + return -1 } // Example -const myArray = [10, 22, 35, 40, 45, 50, - 80, 82, 85, 90, 100] -let n = myArray.length -let x = 90 +const myArray = [10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100] +const n = myArray.length +const x = 90 const fibFinder = fibonacciSearch(myArray, x, n) -console.log("Element found at index:", fibFinder) \ No newline at end of file +console.log('Element found at index:', fibFinder) From cf8c1e7585f48315d6dae36de47ba2522f42219d Mon Sep 17 00:00:00 2001 From: Alhassan Atama Date: Fri, 2 Oct 2020 11:11:19 +0100 Subject: [PATCH 4/4] FIxed Lint issues --- Search/FibonacciSearch.js | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/Search/FibonacciSearch.js b/Search/FibonacciSearch.js index 576bff0e81..fcc8c85619 100644 --- a/Search/FibonacciSearch.js +++ b/Search/FibonacciSearch.js @@ -22,10 +22,10 @@ const fibonacciSearch = (arr, x, n) => { let fib2 = 0 // (K-2)'th Fibonacci Number let fib1 = 1 // (K-1)'th Fibonacci Number. - let fibK = fib2 + fib1 //Kth Fibonacci + let fibK = fib2 + fib1 // Kth Fibonacci /* We want to store the smallest fibonacci number smaller such that - number is greater than or equal to n, we use fibK for this */ + number is greater than or equal to n, we use fibK for this */ while (fibK < n) { fib2 = fib1 fib1 = fibK @@ -35,34 +35,32 @@ const fibonacciSearch = (arr, x, n) => { let offset = -1 /* while there are elements to be checked. We compare arr[fib2] with x. - When fibM becomes 1, fib2 becomes 0 */ + When fibM becomes 1, fib2 becomes 0 */ while (fibK > 1) { - // Check if fibK is a valid location - const i = Math.min(offset + fib2, n - 1) + // Check if fibK is a valid location + const i = Math.min(offset + fib2, n - 1) - /* If x is greater than the value at - index fib2, Partition the subarray array - from offset to i */ + /* If x is greater than the value at + index fib2, Partition the subarray array + from offset to i */ if (arr[i] < x) { fibK = fib1 fib1 = fib2 fib2 = fibK - fib1 offset = i /* If x is greater than the value at - index fib2, cut the subarray array - from offset to i */ - } - else if (arr[i] > x) { + index fib2, cut the subarray array + from offset to i */ + } else if (arr[i] > x) { fibK = fib2 fib1 = fib1 - fib2 fib2 = fibK - fib1 - } + } else { // return index for found element - else { return i } -} + } // comparing the last element with x */ if (fib1 && arr[offset + 1] === x) { @@ -74,6 +72,6 @@ const fibonacciSearch = (arr, x, n) => { // Example const myArray = [10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100] const n = myArray.length -const x = 90 +const x = 90 const fibFinder = fibonacciSearch(myArray, x, n) console.log('Element found at index:', fibFinder)