Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
Java/src/main/java/com/search/FibonacciSearch.java at Development · Ginald/Java · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Small and medium teams
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Ginald
Java
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
Development
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
search
/
FibonacciSearch.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
69 lines (59 loc) · 2.41 KB
Development
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
search
/
FibonacciSearch.java
Copy path
Top
File metadata and controls
Code
Blame
69 lines (59 loc) · 2.41 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package
com
.
search
;
import
static
java
.
lang
.
Math
.
min
;
/**
* Fibonacci search 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. Compared to binary search where the sorted array is divided
* into two equal-sized parts, one of which is examined further, Fibonacci search divides the array into two parts that
* have sizes that are consecutive Fibonacci numbers.
* <p>
* Worst-case performance O(Log n)
* Best-case performance O(1)
* Average performance O(Log n)
* Average space complexity O(1)
*/
public
class
FibonacciSearch
{
/**
* @param array is an array where the element should be found
* @param key is an element which should be found
* @param <T> is any comparable type
* @return The index position of the key in the array, returns -1 for empty array
*/
public
<
T
extends
Comparable
<
T
>>
int
findIndex
(
T
[]
array
,
T
key
) {
int
size
=
array
.
length
;
if
(
size
==
0
)
return
-
1
;
// Initialize the fibonacci numbers
int
fibN1
=
1
;
// (n-1)th Fibonacci term
int
fibN2
=
0
;
// (n-2)th Fibonacci term
int
fibN
=
fibN1
+
fibN2
;
// nth Fibonacci term
// fibN should store the smallest Fibonacci Number greater than or equal to size
while
(
fibN
<
size
) {
fibN2
=
fibN1
;
fibN1
=
fibN
;
fibN
=
fibN2
+
fibN1
;
}
// Marks the eliminated range from front
int
offset
= -
1
;
while
(
fibN
>
1
) {
// Check if fibN2 is a valid location
int
i
=
min
(
offset
+
fibN2
,
size
-
1
);
// If key is greater than the value at index fibN2, cuts the sub-array from offset to i
if
(
array
[
i
].
compareTo
(
key
) <
0
) {
fibN
=
fibN1
;
fibN1
=
fibN2
;
fibN2
=
fibN
-
fibN1
;
offset
=
i
;
}
// If x is greater than the value at index fibN2, cuts the sub-array after i+1
else
if
(
array
[
i
].
compareTo
(
key
) >
0
) {
fibN
=
fibN2
;
fibN1
=
fibN1
-
fibN2
;
fibN2
=
fibN
-
fibN1
;
}
else
return
i
;
// Element found
}
// comparing the last element with key
if
(
fibN1
==
1
&&
array
[
offset
+
1
].
compareTo
(
key
) ==
0
)
return
offset
+
1
;
return
-
1
;
// Element not found
}
}
You can’t perform that action at this time.