-
-
Notifications
You must be signed in to change notification settings - Fork 511
feat: add fibonaccisearch algorithm #244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7a8d2c7
feat: add fibonaccisearch algorithm
Fechuli aed58ba
test: add test for fibonacci search algorithm
Fechuli ab771fb
fix: changed variable names and function return
Fechuli e31d1f1
remove redundant @function
appgurueu ae55fe8
fix tests
appgurueu 5bb6396
fix type
appgurueu 1fe60c4
fix formatting
appgurueu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /** | ||
| * @function fibonacciSearch | ||
| * @description Fibonacci search algorithm for a sorted array. | ||
| * | ||
| * The algorithm searches for a specific value in a sorted array using Fibonacci numbers | ||
| * to divide the array into smaller subarrays. This algorithm is useful for large arrays where | ||
| * the cost of accessing elements is high. | ||
| * | ||
| * @param {number[]} array - sorted list of numbers | ||
| * @param {number} target - target number to search for | ||
| * @return {number | null} - index of the target number in the list, or null if not found | ||
| * @see [FibonacciSearch](https://www.geeksforgeeks.org/fibonacci-search/) | ||
| * @example fibonacciSearch([1,2,3], 2) => 1 | ||
| * @example fibonacciSearch([4,5,6], 2) => null | ||
| */ | ||
|
|
||
| export const fibonacciSearch = ( | ||
| array: number[], | ||
| target: number | ||
| ): number | null => { | ||
| const arrayLength = array.length | ||
| let a = 0; // (n-2)'th Fibonacci No. | ||
| let b = 1; // (n-1)'th Fibonacci No. | ||
| let c = a + b; // n'th Fibonacci | ||
|
|
||
| while (c < arrayLength) { | ||
| a = b; | ||
| b = c; | ||
| c = a + b; | ||
| } | ||
|
|
||
| let offset = -1 | ||
|
|
||
| while (c > 1) { | ||
| let i = Math.min(offset + a, arrayLength - 1); | ||
|
|
||
| if (array[i] < target) { | ||
| c = b; | ||
| b = a; | ||
| a = c - b; | ||
| offset = i; | ||
| } else if (array[i] > target) { | ||
| c = a; | ||
| b = b - a; | ||
| a = c - b; | ||
| } else { | ||
| // Element found then return index | ||
| return i; | ||
| } | ||
| } | ||
|
|
||
| if (b && array[offset + 1] === target) { | ||
| return offset + 1; | ||
| } | ||
|
|
||
| // Element not found then return null | ||
| return null; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { fibonacciSearch } from "../fibonacci_search"; | ||
|
|
||
| describe('Fibonacci search', () => { | ||
| test.each([ | ||
| [[1, 2, 3], 2, 1], | ||
| [[4, 5, 6], 2, -1], | ||
| [[10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100], 85, 8], | ||
| [[], 1, -1], | ||
| [[1], 1, 0], | ||
| [[1, 3, 5, 7, 9, 11, 13], 11, 5], | ||
| [[1, 3, 5, 7, 9, 11, 13], 8, -1] | ||
| ])( | ||
| 'of %o, searching for %o, expected %i', | ||
| (array: number[], target: number, expected: number) => { | ||
| expect(fibonacciSearch(array, target)).toBe(expected); | ||
| } | ||
| ); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.