- Notifications
You must be signed in to change notification settings - Fork25
Added fibonacci search#16
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
4a73197 Added fibonacci search
c2ef611 Fixed import to searches instead of sorting
14bf84b Added lines
1d673d7 Merge branch 'master' into master
abranhe4a883c7 add fibonacci search docs on main readme
abranhece87b43 adding forgotten `,`
abranheFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsallalgorithms/searches/__init__.py
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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| from .binary_search import * | ||
| from .fibonacci_search import * | ||
| from .jump_search import * |
35 changes: 35 additions & 0 deletionsallalgorithms/searches/fibonacci_search.py
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,35 @@ | ||
| # -*- coding: UTF-8 -*- | ||
| # | ||
| # Fibonacci search works for a sorted array. | ||
| # The All ▲lgorithms library for python | ||
| # | ||
| # Contributed by: dieterpl | ||
| # Github: @dieterpl | ||
| # | ||
| def fibonacci_search(arr, query): | ||
| fib2, fib1 = 0, 1 | ||
| fib = fib2 + fib1 | ||
| hi = len(arr) - 1 | ||
| while fib <= hi: | ||
| fib2 = fib1 | ||
| fib1 = fib | ||
| fib = fib2 + fib1 | ||
| offset = -1 | ||
| while fib > 1: | ||
| i = min(offset + fib2, hi) | ||
| if arr[i] < query: | ||
| fib = fib1 | ||
| fib1 = fib2 | ||
| fib2 = fib - fib1 | ||
| offset = i | ||
| elif arr[i] > query: | ||
| fib = fib2 | ||
| fib1 = fib1 - fib2 | ||
| fib2 = fib - fib1 | ||
| else: | ||
| return i | ||
| if fib1 and arr[offset + 1] == query: | ||
| return offset + 1 | ||
| return None |
12 changes: 8 additions & 4 deletionschangelog.md
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
1 change: 1 addition & 0 deletionsdocs/readme.md
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
33 changes: 33 additions & 0 deletionsdocs/searches/fibonacci-search.md
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,33 @@ | ||
| # Fibonacci Search | ||
| In computer science, the Fibonacci search technique is a method of searching a sorted array using a divide and conquer algorithm that narrows down possible locations with the aid of Fibonacci numbers. (Wikipedia) | ||
| ## Install | ||
| ``` | ||
| pip install allalgorithms | ||
| ``` | ||
| ## Usage | ||
| ```py | ||
| from allalgorithms.searches import fibonacci_search | ||
| arr = [-2, 1, 2, 7, 10, 77] | ||
| print(fibonacci_search(arr, 7)) | ||
| # -> 3 | ||
| print(fibonacci_search(arr, 3)) | ||
| # -> None | ||
| ``` | ||
| ## API | ||
| ### fibonacci_search(array, query) | ||
| > Return array index if its found, otherwise returns `None` | ||
| ##### Params: | ||
| - `array`: Sorted Array | ||
| - `query`: Element to search for in the array |
2 changes: 2 additions & 0 deletionsreadme.md
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
15 changes: 12 additions & 3 deletionstests/test_searches.py
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
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.